How to add Environment Appsettings and use in .NET Core Console Application

Updated on: February 24, 2023

appsettings.json is used to get the configuration like ConnectionString, SMTP Email Configuration, Application related settings, but when we deploy our application to particular server like Development Server, Staging Server and Production Server then we are expecting to get the Configuration from particular appsettings.json file like on Staging Server we are expecting the configuration to read from appsettings.Staging.json file and the same for Production we are expecting it to read it from appsettings.Production.json.

First, we will create new Console App, to create new Console App, we can use Visual Studio or we can use Command Prompt as shown below:

From Visual Studio: Open Visual Studio=> Click on "Create a New Project" => from templates select "Console App"=> Click on Next button in the bottom after selecting template=> Give proper Project name and select Location of the project and give Solution name as well and click on Next button=>Select Framework (.NET 5 / .NET 6 / .NET 7) and click on Create button.

From Command Prompt, we can create new .NET Core Console Application using following commands:

F:\SampleApps\DotNetCore\Console>dotnet new console -o SampleConsoleApp
F:\SampleApps\DotNetCore\Console>cd SampleConsoleApp
F:\SampleApps\DotNetCore\Console\SampleConsoleApp>dotnet new sln
F:\SampleApps\DotNetCore\Console\SampleConsoleApp>dotnet sln add SampleConsoleApp.csproj

Now, our .NET Core Console App project is created, here, we are using Visual Studio 2022 and .NET Core 7 for this Application, now we will add following 2 Nuget packages to this new project:

dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.Json
dotnet add package Microsoft.Extensions.Configuration.EnvironmentVariables

Now, we have to add appsettings.json, appsettings.Development.json, appsettings.Staging.json and appsettings.Production.json and also marked them as "Copy Always" in "Copy to Output Directory" in the Properties window as shown below, this step is required, so that these files will get automatically copied to bin folder whenever we are building our project:

The content of the appsettings.Development.json is as below:

{
  "ConnectionString": "Development Connectionstring"
}

Now, in Program.cs, we have written following code, which will set appsettings.json and read Environment also:

    internal class Program
    {
        static void Main(string[] args)
        {
            var environment = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
            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}");
        }
    }

While running code from Visual Studio, we will be using launchSettings.json file which is in Properties folder and we can add more profiles as shown below:

Following is the content of launchSettings.json file:

{
  "profiles": {
    "SampleConsoleApp": {
      "commandName": "Project"
    },
    "DevEnvironment": {
      "commandName": "Project",
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Development"
      }
    },
    "ProdEnvironment": {
      "commandName": "Project",
      "environmentVariables": {
        "DOTNET_ENVIRONMENT": "Production"
      }
    }
  }
}

While running from Visual Studio, we have option to select which Profile we have to run by using following window, it will show all the Profiles from launchSettings.json file:

So if we select, DevEnvironment, then it will set DOTNET_ENVIRONMENT=Development and then it will pick appsettings.Development.json, the same way it will happen for ProdEnvironment, it will pick appsettings.Production.json.

Now, if we are running our Console app .EXE from outside Visual Studio, then we have to run following command manually to set Environment:

F:\SampleApps\DotNetCore\Console\SampleConsoleApp\bin\Debug\net7.0>set DOTNET_ENVIRONMENT=Production

So, finally we have seen multiple ways to set Environment Variables from Visual Studio and outside Visual Studio, hope this article helps you to use Environment based AppSettings.