在 ConfigureServices 方法中访问 IHostingEnvironment

我需要检查在 ConfigureServices方法是否当前托管环境名称是“开发”。

因此,使用 IHostingEnvironment.IsDevelopment()方法对我来说可能是可以的,但是与 Configure 方法不同的是,我没有 IHostingEnvironment env

28211 次浏览

just create a property in the Startup class to persist the IHostingEnvironment. Set the property in the Startup constructor where you already have access, then you can access the property from ConfigureServices

Copied here from question marked as duplicate of this one and deleted. Credit to a-ctor.

If you want to access IHostingEnvironment in ConfigureServices you will have to inject it via the constructor and store it for later access in ConfigureServices:

public class Startup
{
public Startup(IConfiguration configuration, IHostingEnvironment environment)
{
Configuration = configuration;
Environment = environment;
}


public IConfiguration Configuration { get; }


public IHostingEnvironment Environment { get; }


public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();


System.Console.WriteLine($"app: {Environment.ApplicationName}");
}


// rest omitted
}

IHostingEnvironment is deprecated in Core 3.1

        private readonly IWebHostEnvironment _env;
   

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
_env = env;
Configuration = configuration;
}

should do the trick...

Then reference anywhere with _env.IsDevelopment() etc...

If you aren't using a Startup class and are calling .Configure() directly, you can access the IHostingEnvironment or IWebHostEnvironment using GetService:

ASP.NET Core 2.2:

.Configure(app => {
var hostingEnvironment = app.ApplicationServices.GetService<IHostingEnvironment>();
if (hostingEnvironment?.IsDevelopment() == true)
{
app.UseDeveloperExceptionPage();
}


// .. Other stuff
});

ASP.NET Core 3.x:

.Configure(app => {
var hostingEnvironment = app.ApplicationServices.GetService<IWebHostEnvironment>();
if (hostingEnvironment?.IsDevelopment() == true)
{
app.UseDeveloperExceptionPage();
}


// .. Other stuff
});

If you don't have a Startup class (you may be creating a service) you can get the environment from the hostContext in ConfigureServices like so:

 public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
IConfiguration config = hostContext.Configuration;
Configuration = config;


var env = hostContext.HostingEnvironment;
EnvironmentName = env?.EnvironmentName;


...

There are some examples here but none of them seem accurate to the newest release of dotnet, so here we go.

First, in your Startup.cs you should have a reference in your initial constructor like this:

public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
this.Configuration = configuration;
this.Environment = env;
}

We can take the Env and tack it onto ServiceCollection like so:

services.AddSingleton(sp => this.Environment);

Then we can access it in any extension method or other place like so:

var hostEnvironment = services.BuildServiceProvider().GetRequiredService<IHostEnvironment>();


if (hostEnvironment.IsDevelopment())
{
//do something if in dev
}