Multiple Enviroment Support in Asp.net core.




Introduction.

ASP.NET Core has introduce new feature and many more performance enhancement. ASP.NET Core configures app behavior based on the runtime environment using an environment variable.

We can set any value to ASPNETCORE_ENVIRONMENT but three environment value are in-build supported by framework are Development, Staging, and Production. by default value of ASPNETCORE_ENVIRONMENT is Production.


Description.

Let use see how we can set ASPNETCORE_ENVIRONMENT using Visual studio. the environment variable settings can be specified in our project's debug profiles.


Above image shows current environment is set as Development.

when we are modify any settings within the project will be refect in launchSettings.json, launchSettings.json is located into properties folder. The "launchSettings.json" file holds the setting, which is specific to each profile used to launch the Application.


Asp.net core read environment variable value at app startup and store value into IHostingEnvironment.EnvironmentName. The development environment can enable features that shouldn't be exposed in production. the environment setting may be used to define different strategy for different environment Development or Production. for example minified js can be used in production environment and un-minified js can be used in Production.

we can check environment value in startup file using in-build extension method like IsDevelopment, IsProduction or by IsEnvironment(method). For example, IsDevelopment returns true if the environment name is Development, else false. 

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if(env.IsDevelopment())
{
// ToDO Current environment name is "Development"
}
else
{
// ToDO Current environment name is other than "Development"
}
}

Asp.net core application can only use single environment, as value set against ASPNETCORE_ENVIRONMENT variable.


Environment based Startup class and methods.

Asp.net core support environment based startup configuration.  we can programmatically control how your application behaves to environment in startup stage.

However, if a class exists named Startup{EnvironmentName} for example StartupDevelopment, and the ASPNETCORE_ENVIRONMENT environment variable matches that environment startup class, then that Startup class is used. Thus, you could configure Startup for development same way we can define separate for Production environment StartupProduction.

public class StartupDevelopment
{

public IConfiguration Configuration { get; }
public StartupDevelopment(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}


public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{

}
}
}

Once we create environment specific startup classes then next we need to register it into program.cs class as below. we need to read startup file based on environment by using  .UseStartup(Assembly.GetEntryAssembly().FullName)

public class Program
{
public static void Main(string[] args)
{
BuildWebHost(args).Run();
}

public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup(Assembly.GetEntryAssembly().FullName)
.Build();
}


We can also define Configure() and ConfigureServices() method support based on environment eg. Configure{EnvironmentName}(), which are configured within startup class.

public class Startup
{

public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public void ConfigureDevelopmentServices(IServiceCollection services)
{
services.AddMvc();
}


public void ConfigureDevelopment(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{

}
}
}

For example if we define both for development environment then it look like ConfigureDevelopment() and ConfigureDevelopmentServices();

No comments

Powered by Blogger.