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:


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:


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, this is my C# .NET blog. I don't have a comments system, but you can contact me at boriseetgerink at hey dot com.