Central Config - Multi-Machine .net Configuration Manager

Posted on December 15, 2015

CentralConfig is available on NuGet, source at https://github.com/andrevdm/CentralConfig

Overview

CentralConfig is a replacement for the built in .net ConfigurationManager. It makes managing multi-machine configurations simple while, from a usability point of view, looking very similar to ConfigurationManager.

Features

What about the .net config transformer

The .net transformer does a reasonable job but did not quite do everything I required. The examples below should show some of the differences

Using CentralConfig

Install “CentralConfig” from nuget Unless you have created a custom persistor use the mongo settings persistor Set the central config mongo settings Setup DI

app.settings for the mongo persistor

   <appSettings>
     <add key="MongoDB.Server" value="mongodb://localhost:27017" />
     <add key="CentralConfig.MongoDatabase" value="TestConfig" />
   </appSettings>

DI setup

       ObjectFactory.Configure( x =>
       {
           x.For().Use();
           x.For().Use();
       } );

Configure settings in Mongo

       {
           "key" : "TestSetting",
           "machine" : null,
           "value" : "some value"
       }

Use the setting from code

       Console.WriteLine( "test: {0}", ConfigManager.AppSettings["TestSetting"] );

Different Persistors

I’ve always used the mongo persistor but you can easily create new persistors as required. To do so implement the IPersistor interface

        public interface IConfigPersistor
        {
            string ReadAppSetting( string key, string machine, long version );
            string ReadAppSetting( string key, string machine );
            string ReadAppSetting( string key, long version );
            string ReadAppSetting( string key );

            TResult GetSection( string sectionName, string machine, long version );
            TResult GetSection( string sectionName, string machine );
            TResult GetSection( string sectionName, long version );
            TResult GetSection( string sectionName );
        }

Order of matching

When you request a value, CentralConfig will try find the most specific value it can. So it will first look for the setting match both the key and the current machine name, if that does not match then it looks for a value matching on key only

       {
           "key" : "TestSetting",
           "machine" : null,
           "value" : "some value"
       },
       {
           "key" : "TestSetting",
           "machine" : "MYPC",
           "value" : "another value"
       }

Given the mongo settings above when you request “TestSetting” you will get “another value” if your machine name is MYPC or “some value otherwise”