如何在启动中的ConfigureServices方法中获得开发/登台/生产托管环境?

public void ConfigureServices(IServiceCollection services)
{
    // Which environment are we running under?
}

ConfigureServices方法只接受一个IServiceCollection参数。


当前回答

由于还没有完整的复制和粘贴解决方案,基于Joe Audette的回答:

public IWebHostEnvironment Environment { get; }

public Startup(IWebHostEnvironment environment, IConfiguration configuration)
{
   Environment = environment;
   ...
}

public void ConfigureServices(IServiceCollection services)
{
   if (Environment.IsDevelopment())
   {
       // Do something
   }else{
       // Do something
   }
   ...
}

其他回答

宿主环境来自ASPNET_ENV环境变量,在启动过程中使用IHostingEnvironment可用。IsEnvironment扩展方法,或IsDevelopment或IsProduction中相应的方便方法之一。在Startup()中保存你需要的东西,或者在ConfigureServices调用中保存:

var foo = Environment.GetEnvironmentVariable("ASPNET_ENV");

对于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)
        {

你可以很容易地在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...
}

博士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.

如果你需要在你的代码库中某个不容易访问IHostingEnvironment的地方测试这个,另一个简单的方法是这样做的:

bool isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";