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:
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:
The takeaways
- Place global configuration settings in the appsettings.json file.
- Override environment specific settings in the environment appsettings file.
- Deploy application secrets as environment variables.