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).
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.
ASP.NET Core apps configure and launch a host. The host is responsible for app startup and lifetime management. At a minimum, the host configures a server and a request processing pipeline. The host can also set up logging, dependency injection, and configuration.
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(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...
}
Set an environment variable called ASPNETCORE_ENVIRONMENT
with the name of the environment (e.g. Production
). Then do one of two things:
IHostingEnvironment
into Startup.cs
, then use that (env
here) to check: env.IsEnvironment("Production")
. Do not check using env.EnvironmentName == "Production"
!Startup
classes or individual Configure
/ConfigureServices
functions. If a class or the functions match these formats, they will be used instead of the standard options on that environment.
Startup{EnvironmentName}()
(entire class) || example: StartupProduction()
Configure{EnvironmentName}()
|| example: ConfigureProduction()
Configure{EnvironmentName}Services()
|| example: ConfigureProductionServices()
The .NET Core docs describe how to accomplish this. Use an environment variable called ASPNETCORE_ENVIRONMENT
that's set to the environment you want, then you have two choices.
From the docs:
The
IHostingEnvironment
service provides the core abstraction for working with environments. This service is provided by the ASP.NET hosting layer, and can be injected into your startup logic via Dependency Injection. The ASP.NET Core web site template in Visual Studio uses this approach to load environment-specific configuration files (if present) and to customize the app’s error handling settings. In both cases, this behavior is achieved by referring to the currently specified environment by callingEnvironmentName
orIsEnvironment
on the instance ofIHostingEnvironment
passed into the appropriate method.
NOTE: Checking the actual value of env.EnvironmentName
is not recommended!
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 ifenv.EnvironmentName == "Development"
for example).
From the docs:
When an ASP.NET Core application starts, the
Startup
class is used to bootstrap the application, load its configuration settings, etc. (learn more about ASP.NET startup). However, if a class exists namedStartup{EnvironmentName}
(for exampleStartupDevelopment
), and theASPNETCORE_ENVIRONMENT
environment variable matches that name, then thatStartup
class is used instead. Thus, you could configureStartup
for development, but have a separateStartupProduction
that would be used when the app is run in production. Or vice versa.In addition to using an entirely separate
Startup
class based on the current environment, you can also make adjustments to how the application is configured within aStartup
class. TheConfigure()
andConfigureServices()
methods support environment-specific versions similar to theStartup
class itself, of the formConfigure{EnvironmentName}()
andConfigure{EnvironmentName}Services()
. If you define a methodConfigureDevelopment()
it will be called instead ofConfigure()
when the environment is set to development. Likewise,ConfigureDevelopmentServices()
would be called instead ofConfigureServices()
in the same environment.
If you need to test this somewhere in your codebase that doesn't have easy access to the IHostingEnvironment, another easy way to do it is like this:
bool isDevelopment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";
In .NET Core 2.0
MVC app / Microsoft.AspNetCore.All
v2.0.0, you can have environmental specific startup class as described by @vaindil but I don't like that approach.
You can also inject IHostingEnvironment
into StartUp
constructor. You don't need to store the environment variable in Program
class.
public class Startup
{
private readonly IHostingEnvironment _currentEnvironment;
public IConfiguration Configuration { get; private set; }
public Startup(IConfiguration configuration, IHostingEnvironment env)
{
_currentEnvironment = env;
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
......
services.AddMvc(config =>
{
// Requiring authenticated users on the site globally
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
config.Filters.Add(new AuthorizeFilter(policy));
// Validate anti-forgery token globally
config.Filters.Add(new AutoValidateAntiforgeryTokenAttribute());
// If it's Production, enable HTTPS
if (_currentEnvironment.IsProduction()) // <------
{
config.Filters.Add(new RequireHttpsAttribute());
}
});
......
}
}
This can be accomplished without any extra properties or method parameters, like so:
public void ConfigureServices(IServiceCollection services)
{
IServiceProvider serviceProvider = services.BuildServiceProvider();
IHostingEnvironment env = serviceProvider.GetService<IHostingEnvironment>();
if (env.IsProduction()) DoSomethingDifferentHere();
}
per the docs
Configure and ConfigureServices support environment specific versions of the form Configure{EnvironmentName} and Configure{EnvironmentName}Services:
You can do something like this...
public void ConfigureProductionServices(IServiceCollection services)
{
ConfigureCommonServices(services);
//Services only for production
services.Configure();
}
public void ConfigureDevelopmentServices(IServiceCollection services)
{
ConfigureCommonServices(services);
//Services only for development
services.Configure();
}
public void ConfigureStagingServices(IServiceCollection services)
{
ConfigureCommonServices(services);
//Services only for staging
services.Configure();
}
private void ConfigureCommonServices(IServiceCollection services)
{
//Services common to each environment
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With