Configuring environments in .NET console app

March 5th 2021 .NET

ASP.NET Core applications are already set up with support for reading configuration files and switching between different environments. You get none of that when you create a new .NET console application. Fortunately, you can still take advantage of the same NuGet packages add similar support with minimum amount of setup code.

The configuration classes used by ASP.NET Core are available in NuGet packages prefixed by Microsoft.Extensions.Configuration. I installed the following ones for my setup:

With all these packages installed, I could initialize the ConfigurationBuilder with a subset of the default ASP.NET Core setup:

var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var builder = new ConfigurationBuilder()
    .AddJsonFile($"appsettings.json", true, true)
    .AddJsonFile($"appsettings.{environment}.json", true, true)
    .AddEnvironmentVariables();
var configurationRoot = builder.Build();

The above setup will read the configuration values from the appsettings.json file, the environment specific appsettings.*.json file, and the environment variables. The values from the source listed later will overwrite the values from a source listed earlier.

I decided to read the environment name from the same environment variable as ASP.NET Core does (i.e. ASPNETCORE_ENVIRONMENT), although the name isn't all that intuitive.

The following line will map the configuration to a strongly typed class:

var appConfig = configurationRoot.GetSection(nameof(AppConfig)).Get<AppConfig>();

The class must have properties that match individual configuration values to be read, e.g.:

public class AppConfig
{
    public string Environment { get; set; }
}

The above class code and class will map the values from the following configuration file:

{
  "AppConfig": {
    "Environment": "DEV"
  }
}

To switch between different environments in Visual Studio, you can configure profiles on the Debug tab of the Project Properties window:

The Debug tab of the Project Properties window.

I find it even more convenient to edit the profiles in the launchSettings.json placed in the Properties folder of the project:

{
  "profiles": {
    "EnvironmentConfiguration.Cli (dev)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "EnvironmentConfiguration.Cli (prod)": {
      "commandName": "Project",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
      }
    }
  }
}

The file will not be present in a new console app project. But it will be added by Visual Studio as soon as you start editing the profiles in the Project Properties window.

Once you configure the profiles, you can select the profile to run from the Visual Studio toolbar:

Profile selection in Visual Studio toolbar

To see a working example, you can check the code in my GitHub repository.

Although there's no out-of-the-box support for configuration in .NET console apps, you can add it yourself using the same classes as the default setup in ASP.NET Core apps.

Get notified when a new blog post is published (usually every Friday):

If you're looking for online one-on-one mentorship on a related topic, you can find me on Codementor.
If you need a team of experienced software engineers to help you with a project, contact us at Razum.
Copyright
Creative Commons License