Set
Set

Reputation: 49789

Accessing the IHostingEnvironment in ConfigureServices method

I need to check in ConfigureServices method whether the current hosting environment name is 'Development'.

So using IHostingEnvironment.IsDevelopment() method may be ok for me, but unlike in Configure method, I do not have IHostingEnvironment env.

Upvotes: 112

Views: 32551

Answers (6)

FoxDeploy
FoxDeploy

Reputation: 13567

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
   }

Upvotes: 1

Rye bread
Rye bread

Reputation: 1811

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;

...

Upvotes: 2

Mike Christensen
Mike Christensen

Reputation: 91696

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
});

Upvotes: 1

Boycs
Boycs

Reputation: 5688

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

Upvotes: 5

Richard
Richard

Reputation: 109140

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
}

Upvotes: 37

Joe Audette
Joe Audette

Reputation: 36736

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

Upvotes: 141

Related Questions