Set appsettings.json for Dev and Release Environments Automatically in ASP.NET Core

Today in this article we will learn how to set appsettings.json for Dev and Release Environments in ASP.NET Core. You could also set appsettings.json for DEV or TEST/QA or PRODUCTION environment.

Getting Started

Lets create simple ASP.NET Core application.

Add appsettings.json file

As a next step, we shall add the following 3 types of appsettings.json files with configuration details that are specific to DEV, TEST Or STAGING, and PROD.

  • appsettings.Development.json
{    "Customer": {     "CustomerKeyurl": "http://customer-dev/key",     "CustomerdetailsUrl": "http://customer-dev/id",     "Agency": {       "AgencyID": "subvalue1_from_json",       "AccountKey": 200     }   } }                
  • appsettings.test.json or appsettings.staging.json
{    "Customer": {     "CustomerKeyurl": "http://customer-test/key",     "CustomerdetailsUrl": "http://customer-test/id",     "Agency": {       "AgencyID": "subvalue1_from_json",       "AccountKey": 200     }   } }                

appsettings.prod.json

{    "Customer": {     "CustomerKeyurl": "http://customer-prod/key",     "CustomerdetailsUrl": "http://customer-prod/id",     "Agency": {       "AgencyID": "subvalue1_from_json",       "AccountKey": 200     }   } }                

Next step is to enable loading of environment specific configuration.

Dynamically loading environment specific configuration

You can easily load any environment-specific configuration easily without even a single line of code. This is possible by using Generic Host builder i.e CreatDefaultBuilder

This default builder is already enabled in ASP.NET Core 3.1 onwards and load the specific information of configuration by default.

I recommend you to read through below article on the same,

  • Understanding CreatDefaultBuilder in ASP.NET Core

When you use Generic Host Builder, it loads the Configuration from below region,

  • Development
  • Staging or Test
  • Production

In ASP.NET Core app,

public class Program    {        public static void Main(string[] args)        {            CreateWebHostBuilder(args).Build().Run();        }          public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>            WebHost.CreateDefaultBuilder(args)                .UseStartup<Startup>();    }                

Default Runtime Environment

ASP.NET Core reads from the following environment variables when determining runtime environment,

  1. DOTNET_ENVIRONMENT
  2. ASPNETCORE_ENVIRONMENT

The above environment variables can be set on the Target Host machine or Local machine (Developer machine using Visual Studio or VSCode settings for debugging purposes).

If using Cloud as Host – the above variable needs to be set as environment variable using .YAML or . YML or any other mechanism as required by your Cloud provider.

For example, in Azure, you can use Appservices ->Configuration->Application Settings section to add the environment variable. Please refer to this for more details.

Scenarios – If you set DOTNET_ENVIRONMENT as "Production" on the target Host machine, then CreateWebHostBuilder will use the configuration from appsettings.Production.json file

Scenarios – If you set DOTNET_ENVIRONMENT as "Test" on the target Host machine, then CreateWebHostBuilder will use the configuration from appsettings.Test.json file.

Scenarios – If you set DOTNET_ENVIRONMENT as "Staging" on the target Host machine, then CreateWebHostBuilder will use the configuration from appsettings.Staging.json file.

Scenarios – If you set DOTNET_ENVIRONMENT as "Development" on the target Host machine, then CreateWebHostBuilder will use the configuration from appsettings.Development.json file.

So here your package will contain all the configuration files but the runtime will decide which files to use depending on DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT values.

How to use Custom Environment variable

Above as we discussed DOTNET_ENVIRONMENT or ASPNETCORE_ENVIRONMENT are readily available as .NET Core supported generic environment variables.

Developers may want to use own defined variable ?

In such cases, you can define your own Environment variable by adding custom loading of that environment variable with explicitly and dynamically setting up while bootstrapping.

Lets define custom Environment variable as "ENV"

Below is how you can override and load your generics ENV variable for loading specific environment details.

For more detail on custom loading approach please below article,

  • Loading JSON,XML or INI configuraiton in ASP.NET Core

appsettings.json in .NET Core – Console or Desktop app

One can easily use the required configuration in non-host apps like Console or Desktop apps like WPF or Form application. Here one can follow either DI for Loading configuration or a non-DI approach for loading configuration

Please refer below article on how to use CreateDefaultBuilder in Console or Desktop application.

  • Reading apsettings.json Configuration – Part II
  • Reading apsetting.json Configuration- Part I

How to Set Environment for Local Debugging purpose

Below mechanism can only be used for local debugging purpose.

This can be done using Visual Studio or VScode editor easily,

  • In VSCode Use .vscode/launch.jsonfor setting the environment for debugging purposes.
  • In Visual Studio use launchSettings.json or use Porject->Properties->Debug->Enviornment Variable to set the environment for debugging purposes.

Thats all !

Do you have any comments or ideas or any better suggestions to share?

Please sound off your comments below.

Summary

Today in this article, we looked at possible solutions for setting appsettings.json for different Environments automatically in ASP.NET Core supporting different environments like Dev, Test or Production, etc. We also looked at how to set up and override custom generic environment variables if needed.



Please bookmark this page and share this article with your friends and Subscribe to the blog to get a notification on freshly published best practices of software development.