小编典典

如何在 ConfigureServices 中获取 Development/Staging/production

all

如何ConfigureServices在 Startup 的方法中获取 Development/Staging/production Hosting Environment?

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

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


阅读 81

收藏
2022-04-21

共1个答案

小编典典

您可以在 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...
}
2022-04-21