如何在启动中的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参数。
当前回答
宿主环境来自ASPNET_ENV环境变量,在启动过程中使用IHostingEnvironment可用。IsEnvironment扩展方法,或IsDevelopment或IsProduction中相应的方便方法之一。在Startup()中保存你需要的东西,或者在ConfigureServices调用中保存:
var foo = Environment.GetEnvironmentVariable("ASPNET_ENV");
其他回答
博士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.
以防有人也在看这个。在.net core 3+中,这些都已经过时了。更新方式为:
public void Configure(
IApplicationBuilder app,
IWebHostEnvironment env,
ILogger<Startup> logger)
{
if (env.EnvironmentName == Environments.Development)
{
// logger.LogInformation("In Development environment");
}
}
我想在我的服务中加入这种环境。这真的很容易做到!我只是像这样把它注入到构造函数中:
private readonly IHostingEnvironment _hostingEnvironment;
public MyEmailService(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
现在在稍后的代码中,我可以这样做:
if (_hostingEnvironment.IsProduction()) {
// really send the email.
}
else {
// send the email to the test queue.
}
编辑:
上面的代码是用于。net Core 2的。对于版本3,您将使用IWebHostEnvironment。
这可以在没有任何额外属性或方法参数的情况下完成,如下所示:
public void ConfigureServices(IServiceCollection services)
{
IServiceProvider serviceProvider = services.BuildServiceProvider();
IHostingEnvironment env = serviceProvider.GetService<IHostingEnvironment>();
if (env.IsProduction()) DoSomethingDifferentHere();
}
对于blazor服务器应用程序,我这样做:Startup.cs直接在命名空间声明add
namespace myProjectName.Server
public static class myGlobals
{
public static bool isDevelopment = true;
}
Startup.cs找到Configure方法和env. cs的现有检查。并将上面声明的静态IsDevelopment设置为true或false。
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
myGlobals.isDevelopment = true;
}
else false
在ApplicationUser中初始化数据库连接或其他任何地方
if (myGlobals.isDevelopment)
{