Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Accessing the IHostingEnvironment in ConfigureServices method

Tags:

asp.net-core

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.

like image 939
Set Avatar asked Jun 06 '16 14:06

Set


People also ask

How do you check if current environment is development or not?

If you need to check whether the application is running in a particular environment, use env. IsEnvironment("environmentname") since it will correctly ignore case (instead of checking if env. EnvironmentName == "Development" for example).

How do I create an environment in ConfigureServices?

Fortunately for us, the answer is simple. You can add it to your Startup constructor: public Startup(IConfiguration configuration, IHostingEnvironment environment) . This will inject an IHostingEnvironment that you can set as a property available to any Startup method.

What is IHostingEnvironment?

The IHostingEnvironment is an interface for . Net Core 2.0. The IHostingEnvironment interface need to be injected as dependency in the Controller and then later used throughout the Controller. The IHostingEnvironment interface have two properties.

What is the use of ConfigureServices method of startup class?

The startup class contains two methods: ConfigureServices(): Registers the services that your application will need. Configure(): Configures the middleware pipeline that controls how the application processes the HTTP requests and sends the response.


1 Answers

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

like image 132
Joe Audette Avatar answered Nov 14 '22 14:11

Joe Audette