如何ConfigureServices在 Startup 的方法中获取 Development/Staging/production Hosting Environment?
ConfigureServices
public void ConfigureServices(IServiceCollection services) { // Which environment are we running under? }
该ConfigureServices方法只接受一个IServiceCollection参数。
IServiceCollection
您可以在 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... }