Controlling Application Behavior Across Multiple Environments With ASP.NET Core

Introduction
 
ASP.NET Core has introduced many new features. One of them is to control Application behavior across multiple environments like development, staging, and production. We can use environment variable to define or configure the Application is running in.

The environment can be set to any value. In this example, we will use development, staging and production. We can detect the current setting of an environment variable programmatically. The environment variable name is case sensitive for LINUX OS and they are case-insensitive for Windows and Mac operating system.
 
Using the following helps to define the way. We can set the environment variable in Windows 10 operating system.
 
 
The same thing can be achieved by the command prompt or Windows PowerShell, using the “set” and “$env:” command.
 
 
When we are using Visual Studio, the environment variable settings can be specified in our project's debug profiles.
 
 
 
When we modify the default setting within the project, the changes are persisted in launchSettings.json in the Properties folder. The "launchSettings.json" file holds the setting, which is specific to each profile used to launch the Application.
 
 
 
Determine the environment
 
The IHostingEnvironment Service provides core abstraction for accessing the environment variables and these Services are at hosting layer, and can be injected into startup class. The environment setting may be used to define error handling strategy for development and production. We can get the value of the current specified environment by calling EnviromentName (property) or IsEnvironment (method) of instance of IHostingEnvironment. HostingEnvironmentExtensions class has the extension methods for checking current hosting environment name for production, staging and development. These methods return Boolean values, return true if they match an environment name else they return false. For example, IsDevelopment returns true if the environment name is Development, else false.
  1. if(env.IsDevelopment())  
  2. {  
  3.     // ToDO Current environment name is "Development"  
  4. }  
  5. else  
  6. {  
  7.     // ToDO Current environment name is other than "Development"  
  8. }  
Apart from this, we can also use the Startup class based on the environment. The Configure and ConfigureServices methods support the environment specific versions in same startup class. The format for naming is Configure{EnvironmentName} and Configure{EnvironmentName}Services(). For example, if we define a method ConfigureDevelopment, the system will call this method instead of configure; when an environment is set to development. 
  1. public void ConfigureDevelopment(IApplicationBuilder app, IHostingEnvironment env)  
  2. {  
  3.     // ToDO  
  4. }  
  5. public void ConfigureProduction(IApplicationBuilder app, IHostingEnvironment env)  
  6. {  
  7.     // ToDO  
  8. }  
Determine the value of an environment variable
 
Using GetEnvironmentVariable method of an Environment class, we can get the value of environment variable in ASP.NET Core Application.
Example
  1. var envVarValue = System.Environment.GetEnvironmentVariable("test1");  
Summary
 
ASP.NET Core allows us to set different configuration based on an environment. The environment variables are case insensitive for Windows and Mac OS whereas they are case sensitive for Linux OS. The environment variables are very useful in controlling the behavior of an Application in multiple environments.