如何在启动中的ConfigureServices方法中获得开发/登台/生产托管环境?
public void ConfigureServices(IServiceCollection services)
{
// Which environment are we running under?
}
ConfigureServices方法只接受一个IServiceCollection参数。
如何在启动中的ConfigureServices方法中获得开发/登台/生产托管环境?
public void ConfigureServices(IServiceCollection services)
{
// Which environment are we running under?
}
ConfigureServices方法只接受一个IServiceCollection参数。
当前回答
博士TL;
设置一个名为ASPNETCORE_ENVIRONMENT的环境变量,使用环境的名称(例如Production)。然后做以下两件事中的一件:
Inject IHostingEnvironment into Startup.cs, then use that (env here) to check: env.IsEnvironment("Production"). Do not check using env.EnvironmentName == "Production"! Use either separate Startup classes or individual Configure/ConfigureServices functions. If a class or the functions match these formats, they will be used instead of the standard options on that environment. Startup{EnvironmentName}() (entire class) || example: StartupProduction() Configure{EnvironmentName}() || example: ConfigureProduction() Configure{EnvironmentName}Services() || example: ConfigureProductionServices()
完整的解释
. net Core文档描述了如何实现这一点。使用一个名为ASPNETCORE_ENVIRONMENT的环境变量,它被设置为您想要的环境,然后您有两个选择。
检查环境名称
从文档中可以看出:
The IHostingEnvironment service provides the core abstraction for working with environments. This service is provided by the ASP.NET hosting layer, and can be injected into your startup logic via Dependency Injection. The ASP.NET Core web site template in Visual Studio uses this approach to load environment-specific configuration files (if present) and to customize the app’s error handling settings. In both cases, this behavior is achieved by referring to the currently specified environment by calling EnvironmentName or IsEnvironment on the instance of IHostingEnvironment passed into the appropriate method.
注意:检查env的实际值。不建议使用“EnvironmentName”!
如果你需要检查应用程序是否在特定的环境中运行,请使用env. isenvironment ("environmentname"),因为它将正确地忽略大小写(而不是检查env. isenvironment是否在特定的环境中运行)。例如,EnvironmentName == "Development")。
使用单独的类
从文档中可以看出:
When an ASP.NET Core application starts, the Startup class is used to bootstrap the application, load its configuration settings, etc. (learn more about ASP.NET startup). However, if a class exists named Startup{EnvironmentName} (for example StartupDevelopment), and the ASPNETCORE_ENVIRONMENT environment variable matches that name, then that Startup class is used instead. Thus, you could configure Startup for development, but have a separate StartupProduction that would be used when the app is run in production. Or vice versa. In addition to using an entirely separate Startup class based on the current environment, you can also make adjustments to how the application is configured within a Startup class. The Configure() and ConfigureServices() methods support environment-specific versions similar to the Startup class itself, of the form Configure{EnvironmentName}() and Configure{EnvironmentName}Services(). If you define a method ConfigureDevelopment() it will be called instead of Configure() when the environment is set to development. Likewise, ConfigureDevelopmentServices() would be called instead of ConfigureServices() in the same environment.
其他回答
从ASP开始。NET Core 3.0中,从ConfigureServices和Configure中访问环境变量要简单得多。
只需将IWebHostEnvironment注入到启动构造函数本身。像这样…
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
Configuration = configuration;
_env = env;
}
public IConfiguration Configuration { get; }
private readonly IWebHostEnvironment _env;
public void ConfigureServices(IServiceCollection services)
{
if (_env.IsDevelopment())
{
//development
}
}
public void Configure(IApplicationBuilder app)
{
if (_env.IsDevelopment())
{
//development
}
}
}
参考:https://learn.microsoft.com/en - us/aspnet/core/fundamentals/environments?view=aspnetcore - 3.0 # inject-iwebhostenvironment-into-the-startup-class
宿主环境来自ASPNET_ENV环境变量,在启动过程中使用IHostingEnvironment可用。IsEnvironment扩展方法,或IsDevelopment或IsProduction中相应的方便方法之一。在Startup()中保存你需要的东西,或者在ConfigureServices调用中保存:
var foo = Environment.GetEnvironmentVariable("ASPNET_ENV");
如果你需要在你的代码库中某个不容易访问IHostingEnvironment的地方测试这个,另一个简单的方法是这样做的:
bool isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
另一种方法是使用configuration ["ASPNETCORE_ENVIRONMENT"]直接从配置中读取环境名称。这适用于任何可以访问配置的地方。
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
Console.WriteLine(Configuration["ASPNETCORE_ENVIRONMENT"]);
}
前提条件是主机是用program .cs中的host . createdefaultbuilder()创建的,这是ASP. cs的默认值。NET Core 3.0(和5.0)web应用程序。如果使用其他构建器,则可以使用program .cs中的AddEnvironmentVariables()添加envars。
你可以很容易地在ConfigureServices中访问它,只是在Startup方法期间将它持久化到一个属性,该方法首先被调用并将其传递进来,然后你可以从ConfigureServices访问该属性。
public Startup(IWebHostEnvironment env, IApplicationEnvironment appEnv)
{
...your code here...
CurrentEnvironment = env;
}
private IWebHostEnvironment CurrentEnvironment{ get; set; }
public void ConfigureServices(IServiceCollection services)
{
string envName = CurrentEnvironment.EnvironmentName;
... your code here...
}