Maintaining App Config For Dev, QA And Prod Regions

Recently, I was working with a client who has just started with the adoption of cloud technologies and they have a long roadmap before they reach Kubernetes or a container-based development approach. 
 
There were a few batch jobs that I was supposed to write, that would perform some push/pull from the existing LOBs to Azure. This was to minimize my efforts so that I don't have to maintain different configs for each region like DEV, QA, and PROD. I wrote a custom config section to be able to slice the different app settings based on the region. Here are some details and codes.

Custom Config Section Entry in the App.Config  
  1. <configSections>  
  2.     <section name ="serverConfiguration" type ="ServerConfiguration.ServerSettingHandler, ServerConfiguration"/>  
  3.   </configSections>  
  4.   
  5. <serverConfiguration>  
  6.     <server name ="Dev">  
  7.       <settings>  
  8.         <add key ="ck" value ="ckDEV"   />  
  9.         <add key ="abc" value ="mlDEV"   />  
  10.         <add key ="cde" value ="p3DEV"   />  
  11.         <add key ="efg" value ="oppDEV"   />  
  12.       </settings>  
  13.     </server>  
  14.     <server name ="QA">  
  15.       <settings>  
  16.         <add key ="ck" value ="ckQA"   />  
  17.         <add key ="abc" value ="mlQA"   />  
  18.         <add key ="cde" value ="p3QA"   />  
  19.         <add key ="efg" value ="oppQA"   />  
  20.       </settings>  
  21.     </server>  
  22.     <server name ="PRD">  
  23.       <settings>  
  24.         <add key ="ck" value ="ckPRD"   />  
  25.         <add key ="abc" value ="mlPRD"   />  
  26.         <add key ="cde" value ="p3PRD"   />  
  27.         <add key ="efg" value ="oppPRD"   />  
  28.       </settings>  
  29.     </server>  
  30.   </serverConfiguration>  
Fetch the Specific Setting Value 
  1.  var server = ServerConfigManager.GetServerSetting("dev","ck");  
  2.   
  3.  Console.WriteLine(server);  
  4.   
  5. // Prints   
  6. // ckDEV
Fetch the All Setting Value
  1. var server = ServerConfigManager.GetServer("Dev");             
  2.     
  3.            foreach (KeyValuePair<string, string> setting in server?.Settings)    
  4.            {    
  5.                Console.WriteLine($"Key : {setting.Key} Value : {setting.Value}");    
  6.            }    
  7. // Prints   
  8. /* 
  9. Key : ck Value : ckDEV 
  10. Key : abc Value : mlDEV 
  11. Key : cde Value : p3DEV 
  12. Key : efg Value : oppDEV 
  13. */  
Other Wrappers

If you decide to name the server with the Machine name, then you can also make use of the below method for fetching the details.
  1. // The GetCurrentServer uses the Environment.MachineName to look in the Config sections and Fetches the value.  
  2. var server = ServerConfigManager.GetCurrentServer()   
Attached code has all the source files.
 
I know this has been available in C# for almost a decade or so, but this blog may be a good read to refresh your knowledge on this old technique.