How to get the Development/Staging/production Hosting Environment in ConfigureServices

前端 未结 12 1858
时光取名叫无心
时光取名叫无心 2020-12-04 15:51

How do I get the Development/Staging/production Hosting Environment in the ConfigureServices method in Startup?

public void ConfigureServices(IS         


        
12条回答
  •  一向
    一向 (楼主)
    2020-12-04 16:27

    You can easily access it in ConfigureServices, just persist it to a property during Startup method which is called first and gets it passed in, then you can access the property from ConfigureServices.

    public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        ...your code here...
        CurrentEnvironment = env;
    }
    
    private IHostingEnvironment CurrentEnvironment{ get; set; } 
    
    public void ConfigureServices(IServiceCollection services)
    {
        string envName = CurrentEnvironment.EnvironmentName;
        ... your code here...
    }
    

提交回复
热议问题