Published: 2020.10.23
Category: C#
Master Configuration
I am an author of many public projects, whenever I am publishing something I face an issue with configuration. Generally, I don’t like maintaining config in the system variables so I needed to think about something different.
My configuration:
- shouldn’t be committed to the public repository
- should be placed in a private repository
- should be easy to change and maintain, locally and on the production server
The idea of master configuration: – store all configuration in the separate git repository
When I am writing code very often I use configuration which shouldn’t be shared in the public git repository. On the other hand, it is not convenient for me to store configuration in the environment variables.
Idea MasterConfiguration is to store the configuration of all applications in one place which could be pushed to the private git repository.
In detail: – you set up two environment variables for all application, one informing that you use MasterConfiguration, second that points to a directory with configuration files – application during startup takes configuration from that place and read critical information – this place can be the second git repository, which is private – it is not the most secured way of storing configuration, but it is a convenient option
Master Configuration usage
Environment setup
- Environment variable ASPNETCORE_ENVIRONMENT needs to be set to
MasterConfiguration
- Environment variable MasterConfigurationPath needs to point to the directory with configuration (for my case is D:.Configuration)
- In the directory mentioned above JSON file with configuration needs to be placed. JSON file should be called the main project file, for example, ProductivityTools.Purchase.Api.json. The name can be changed (checkout info below).
To setup environment variable and file you can also use the PowerShell MasterConfiguration module, which automates those actions. http://productivitytools.tech/powershell-master-configuration/ Cmdlet Set-MasterConfiguration
Application setup
WebApi
To the IConfigurationBuilder
add AddMasterConfiguration
extension method
public static IHostBuilder CreateHostBuilder(string[] args) => Host.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { config.AddMasterConfiguration(); }) .ConfigureWebHostDefaults(webBuilder => { webBuilder.UseStartup<Startup>(); });
To use configuration just use standard DI with IConfiguration interface.
public class TestUsers : ITestUsers { private readonly IConfiguration configuration; public TestUsers(IConfiguration configuration) { this.configuration = configuration; } private string GetPawelPassword() { var s = this.configuration["pawelPassword"]; return s; } }
Console applications
IConfigurationRoot configuration = new ConfigurationBuilder() .AddMasterConfiguration() .Build(); var r = configuration["Region"]; return r;
Legacy applications
Install nuget: Microsoft.Extensions.Configuration.Json
IConfigurationRoot configuration = new ConfigurationBuilder() .AddMasterConfiguration() .Build(); var r = configuration["Region"]; return r;
Development scenario
If you want to use MasterConfiguration file during development you should set up two environment variables: – ASPNETCORE_ENVIRONMENT – MasterConfiguration (instead of Development) – MasterConfigurationPath – Path to MasterConfiguration file
Or you could use Force flag described below

Customization
Force flag
If we don’t want to a setup environment variable (for example we would like to use it only for some simple validation) we can pass true to the force flag of the AddMasterConfiguration method. Then MasterConfiguration file will be added always to the configuration providers. (It doesn’t mean that it will use always data from the master configuration file. If you add another provider after AddMasterConfiguration the last file will override the master configuration file)