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.
- 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 variable ASPNETCORE_ENVIRONMENT needs to be set to
- 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
AddMasterConfiguration extension method
To use configuration just use standard DI with IConfiguration interface.
Install nuget: Microsoft.Extensions.Configuration.Json
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
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)