Productivity Tools

Tools written by an IT business manager, a passionate developer.

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:

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

To setup environment variable and file you can also use the PowerShell MasterConfiguration module, which automates those actions. Cmdlet Set-MasterConfiguration

Application setup


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

Generate override


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)

public static IHostBuilder CreateHostBuilder(string[] args) =>     Host.CreateDefaultBuilder(args)          .ConfigureAppConfiguration((hostingContext, config) =>          {              config.AddMasterConfiguration(force: true);          })         .ConfigureWebHostDefaults(webBuilder =>         {             webBuilder.UseStartup<Startup>();         });
Close Bitnami banner