[英]Proper use of IsDevelopement() on self hosted .net core 2.2 application
I have a self-hosted .NET core 2.2 console app that does not use the web host builder as I do not need HTTP endpoints for this service. 我有一个自托管的.NET Core 2.2控制台应用程序,该应用程序不使用Web主机构建器,因为我不需要此服务的HTTP端点。
I'm trying to leverage the environment variable through the IsDevelopment()
method of the hosting environment but it always comes back as Production . 我正在尝试通过托管环境的
IsDevelopment()
方法来利用环境变量,但是它总是以Production形式返回。
The following is how I've setup my host builder. 以下是我设置主机构建器的方式。 I have an environment variable called
ASPNETCORE_ENVIRONMENT
with a value of Development which leads me to ask two questions. 我有一个名为
ASPNETCORE_ENVIRONMENT
的环境变量,其值为Development ,这使我提出两个问题。
ASPNETCORE_ENVIRONMENT
since my app is not an ASP.NET core application? ASPNETCORE_ENVIRONMENT
以外的其他环境变量? I realize I could probably write code just before building the HostBuilder
that explicitly looks for an environment variable and set the environment manually, but ASP.NET Core seems to hook this up behind the scenes so I wanted to see if there was a way to get a similar behavior when I'm not using the web host builder. 我意识到我可能可以在构建
HostBuilder
之前编写代码,该HostBuilder
明确寻找环境变量并手动设置环境,但是ASP.NET Core似乎将其隐藏在幕后,所以我想看看是否有一种获取方法当我不使用Web Host Builder时,会发生类似的行为。
private static IHost BuildEngineHost(string[] args)
{
var engineBuilder = new HostBuilder()
.ConfigureAppConfiguration((hostContext, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddEnvironmentVariables();
if(hostContext.HostingEnvironment.IsDevelopment())
config.AddUserSecrets<EngineOptions>();
})
.ConfigureServices((hostContext, services) =>
{
services.Configure<EngineOptions>(hostContext.Configuration.GetSection("EngineOptions"));
services.AddHostedService<EtlEngineService>();
})
.ConfigureLogging((hostContext, logging) =>
{
logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
logging.AddConsole();
});
return engineBuilder.Build();
}
UPDATE: The following is needed to configure the host before configuration the application 更新:在配置应用程序之前,需要以下配置主机
.ConfigureHostConfiguration(config =>
{
config.AddCommandLine(args);
config.AddEnvironmentVariables();
})
This is called before .ConfigureAppConfiguration() and is loaded from any variable called "Environment" which means I don't have to use ASPNET_ENVIRONMENT. 这是在.ConfigureAppConfiguration()之前调用的,并且是从任何称为“环境”的变量加载的,这意味着我不必使用ASPNET_ENVIRONMENT。
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2
What is the proper way to have this set when building my own host so that I can conditionally add user secrets to my configuration when building the host?
在构建自己的主机时进行设置的正确方法是什么,以便在构建主机时可以有条件地向配置中添加用户密码?
The proper way is to not have all the lines of code you currently have in your BuildEngineHost
method. 正确的方法是不要在
BuildEngineHost
方法中拥有当前拥有的所有代码行。 If you're using ASP.Net Core 2.2, those settings you've wrote are already set for you. 如果您使用的是ASP.Net Core 2.2,则已经为您设置了编写的那些设置。 In your
Program.cs
file you should just have this: 在您的
Program.cs
文件中,您应该仅具有以下内容:
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>();
}
When you look at CreateDefaultBuilder
method implementation on GitHub you will see what you're trying to do is already done by default. 当您查看GitHub上的
CreateDefaultBuilder
方法实现时,默认情况下您将尝试执行的操作已经完成。 This is the implementation of CreateDefaultBuilder
: 这是
CreateDefaultBuilder
的实现:
public static IWebHostBuilder CreateDefaultBuilder(string[] args)
{
var builder = new WebHostBuilder();
if (string.IsNullOrEmpty(builder.GetSetting(WebHostDefaults.ContentRootKey)))
{
builder.UseContentRoot(Directory.GetCurrentDirectory());
}
if (args != null)
{
builder.UseConfiguration(new ConfigurationBuilder().AddCommandLine(args).Build());
}
builder.ConfigureAppConfiguration((hostingContext, config) =>
{
var env = hostingContext.HostingEnvironment;
config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
if (env.IsDevelopment())
{
var appAssembly = Assembly.Load(new AssemblyName(env.ApplicationName));
if (appAssembly != null)
{
config.AddUserSecrets(appAssembly, optional: true);
}
}
config.AddEnvironmentVariables();
if (args != null)
{
config.AddCommandLine(args);
}
})
.ConfigureLogging((hostingContext, logging) =>
{
logging.AddConfiguration(hostingContext.Configuration.GetSection("Logging"));
logging.AddConsole();
logging.AddDebug();
logging.AddEventSourceLogger();
}).
UseDefaultServiceProvider((context, options) =>
{
options.ValidateScopes = context.HostingEnvironment.IsDevelopment();
});
ConfigureWebDefaults(builder);
return builder;
}
Anyway if you don't want to use this implementation so to answer to your 2nd question you need to use add this line: 无论如何,如果您不想使用此实现,因此要回答第二个问题,则需要使用以下行:
config.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true);
Just after this line: 在此行之后:
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
env
variable is a type IHostingEnvironment
that need to be injected in your BuildEngineHost
method. env
变量是一种IHostingEnvironment
类型,需要注入到BuildEngineHost
方法中。
I was able to have the hosting environment initialized with the correct values by calling ConfigureHostConfiguration() before AppConfiguration which properly sets the environment values in the host which I came across in the following doc from Microsoft. 我可以通过在AppConfiguration之前调用ConfigureHostConfiguration()初始化具有正确值的托管环境,从而在我从以下Microsoft文档中找到的主机中正确设置环境值。
https://docs.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2 https://docs.microsoft.com/zh-cn/aspnet/core/fundamentals/host/generic-host?view=aspnetcore-2.2
private static IHost BuildEngineHost(string[] args)
{
var engineBuilder = new HostBuilder()
.ConfigureHostConfiguration(config =>
{
config.AddEnvironmentVariables();
config.AddCommandLine(args);
})
.ConfigureAppConfiguration((hostContext, config) =>
{
config.SetBasePath(Directory.GetCurrentDirectory());
config.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
config.AddEnvironmentVariables();
if(hostContext.HostingEnvironment.IsDevelopment())
config.AddUserSecrets<EngineOptions>();
})
.ConfigureServices((hostContext, services) =>
{
services.Configure<EngineOptions>(hostContext.Configuration.GetSection("EngineOptions"));
services.AddHostedService<EtlEngineService>();
})
.ConfigureLogging((hostContext, logging) =>
{
logging.AddConfiguration(hostContext.Configuration.GetSection("Logging"));
logging.AddConsole();
});
return engineBuilder.Build();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.