Explain the ASP.NET Core support for multiple environments for development and production


Running an application in production for live customers is very different from running it when you are developing it on your local machine. In production, your application is hosted on a server which has very different configurations and specifications than your computer. Various services that your application talks to, such as databases or external APIs change for production.

By letting the application know which environment it’s running, you can vary the application’s behavior. ASP.NET Core makes it easy to manage various environments effortlessly. You can configure different configuration settings for different environments, and tweak them without having to recompile the application. This lets you easily change the environment.

There are three parts to managing environments for your application, identifying the environment, loading different configuration settings based on the environment, and changing the environment.

To determine the runtime environment, ASP.NET Core uses environment variables named DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT when the ConfigureWebHostDefaults method is called. The second environment variable overrides the first one.

If the application doesn’t find an ASPNETCORE_ENVIRONMENT variable when it starts, it defaults to a production environment. That means, unless you set up the variable on the deployment machine explicitly to development, all ASP.NET Core applications start in the production mode.

You can also use the launchSettings.json file to set up the hosting environment. This file resides in the Properties folder, and defines profiles for running the application. Here’s an example of a typical launchSettings.json file.

{
   "iisSettings": {
      "windowsAuthentication": false,
      "anonymousAuthentication": true,
      "iisExpress": {
         "applicationUrl": "http://localhost:17456",
         "sslPort": 44394
      }
   },
   "profiles": {
      "IIS Express": {
         "commandName": "IISExpress",
         "launchBrowser": true,
         "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
         }
      },
      "TutorialsPoint": {
         "commandName": "Project",
         "dotnetRunMessages": "true",
         "launchBrowser": true,
         "applicationUrl": "https://localhost:5001;http://localhost:5000",
         "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
         }
      }
   }
}

Since an ASP.NET Core application starts as a console application, the current hosting environment is logged to the console upon startup.

➜ TutorialsPoint dotnet run
   Building...
info: Microsoft.Hosting.Lifetime[0]
   Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
   Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[0]
   Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
   Hosting environment: Development
info: Microsoft.Hosting.Lifetime[0]
   Content root path: /TutorialsPoint

The IWebHostEnvironment parameter passed to the Configure() method in the Startup class provides helper methods named IsDevelopment(), IsStaging(), etc. to easily identify the environment. It also has a method named IsEnvironment(envName) that takes a string and returns if the environment name matches that string. You can use it to identify any custom environments.

Though you can use any value to specify an environment, out-of-the-box ASP.NET Core provides three values:

  • Development
  • Staging
  • Production
public void Configure(IApplicationBuilder app, IWebHostEnvironment env){
   if (env.IsDevelopment()){
      app.UseDeveloperExceptionPage();
   }

   if (env.IsProduction() || env.IsStaging() || env.IsEnvironment("Staging_2")){
      app.UseExceptionHandler("/Error");
   }

   app.UseHttpsRedirection();
   app.UseStaticFiles();

   app.UseRouting();

   app.UseAuthorization();

   app.UseEndpoints(endpoints =>{
      endpoints.MapRazorPages();
   });
}

ASP.NET Core determines the environment early in the startup process, even before the configuration is set up. This allows you to dynamically change the configuration providers based on the environment.

Updated on: 22-Jun-2021

845 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements