Use app.config to read Environment and set particular appsettings.{environment}.json in .NET Core
In .NET Core project, there are multiple ways to set Environment Variables, so we can use app.config file when we are publishing the Console Application to our Development, Staging or Production server, so that if Environment variable is empty we can take which environment to use from app.config Appsettings, we will see that in this article below. Now, we will begin with creating our Console Application.
For this Article, we are using Visual Studio 2022 and .NET 7 and we have created a Console application to see the usage of app.config file, we can create Console App from Visual Studio or also with below .NET CLI Commands as shown below:
F:\SampleApps\DotNetCore\Console>dotnet new console -o AppConfigEnvironmentApp
F:\SampleApps\DotNetCore\Console>cd AppConfigEnvironmentApp
F:\SampleApps\DotNetCore\Console\AppConfigEnvironmentApp>dotnet new sln
F:\SampleApps\DotNetCore\Console\AppConfigEnvironmentApp>dotnet sln add AppConfigEnvironmentApp.csproj
Now, we will add app.config file and also add 4 files as "appsettings.json, appsettings.Development.json, appsettings.Staging.json and appsettings.Production.json" and add the following content:
//Contents of app.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="DOTNET_ENVIRONMENT" value="Production"/>
</appSettings>
</configuration>
Also, we will add contents of all appsettings.json files as shown below:
//Contents of appsettings.json
{
"ConnectionString": "Local Connectionstring"
}
//Contents of appsettings.Development.json
{
"ConnectionString": "Development Connectionstring"
}
//Contents of appsettings.Staging.json
{
"ConnectionString": "Staging Connectionstring"
}
//Contents of appsettings.Production.json
{
"ConnectionString": "Production Connectionstring"
}
We should set "Copy to Output Directory" option to "Copy if newer" of these above 4 appsettings.json file as well as app.config as shown below:
Now, add the following 4 Nuget packages:
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json
dotnet add package Microsoft.Extensions.Configuration.EnvironmentVariables
dotnet add package System.Configuration.ConfigurationManager
Now, add the below content in Program.cs file to read Environment variable and use particular appsettings.{environment}.json file based on Environment
using Microsoft.Extensions.Configuration;
internal class Program
{
static void Main(string[] args)
{
var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
if (string.IsNullOrEmpty(environment))
{
environment = System.Configuration.ConfigurationManager.AppSettings["DOTNET_ENVIRONMENT"];//using app.config file to get the Environment Variable
}
var builder = new ConfigurationBuilder()
.AddJsonFile($"appsettings.json", true, true)
.AddJsonFile($"appsettings.{environment}.json", true, true)
.AddEnvironmentVariables();
var config = builder.Build();
string connectionString = config["ConnectionString"];//Getting Connectionstring from appsettings
Console.WriteLine($"Environment is: {environment}");
Console.WriteLine($"Connection String is:{connectionString}");
}
}
Now, we will run this application and see what output we will get, currently we have set the app.config appsettings to "Production" value, so it will read the data from appsettings.Production.json instead of from any other appsettings.{environment}.json file:
I hope that you have understood how to add app.config and read Environment variable from app.config and use it to set out Application's Environment variable.