Boris Eetgerink

April 28, 2023

.NET Configuration sources

My knowledge of application configuration goes way back to .NET full framework's web.config transformations. I write this short post to see how flexible the appsettings.json files are in combination with application secrets.

Let's say I have the following options file:

public class ExternalServiceOptions
{
    public const string ExternalService = "ExternalService";

    public string Name { get; set; } = string.Empty;
    public string Environment { get; set; } = string.Empty;
    public string Key { get; set; } = string.Empty;
}

Then in my appsettings.json I have the following section:

"ExternalService": {
  "Name": "External Service",
  "Environment": "Unspecified"
}

And in my appsettings.Development.json I have the following:

"ExternalService": {
  "Environment": "Development"
}

And I have an application secret set as environment variable like this:

setx ExternalService:Key EnvVarSecret /m

Finally I register the options like so:

builder.Services.Configure<ExternalServiceOptions>(builder.Configuration.GetSection(ExternalServiceOptions.ExternalService));

Now I expect the Name property to be set to "External Service" with the property from appsettings.json, the Environment property to be set to "Development" with the property from appsettings.Development.json and the Key property to be set to "EnvVarSecret" from the environment variable.

It's great to see that it works as expected:

ExternalServiceOptions.png


The takeaways

  1. Place global configuration settings in the appsettings.json file.
  2. Override environment specific settings in the environment appsettings file.
  3. Deploy application secrets as environment variables.

About Boris Eetgerink

Hey, thanks for reading my blog. Subscribe below for future posts like this one and check my personal site in order to contact me.