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
- <configSections>
- <section name ="serverConfiguration" type ="ServerConfiguration.ServerSettingHandler, ServerConfiguration"/>
- </configSections>
-
- <serverConfiguration>
- <server name ="Dev">
- <settings>
- <add key ="ck" value ="ckDEV" />
- <add key ="abc" value ="mlDEV" />
- <add key ="cde" value ="p3DEV" />
- <add key ="efg" value ="oppDEV" />
- </settings>
- </server>
- <server name ="QA">
- <settings>
- <add key ="ck" value ="ckQA" />
- <add key ="abc" value ="mlQA" />
- <add key ="cde" value ="p3QA" />
- <add key ="efg" value ="oppQA" />
- </settings>
- </server>
- <server name ="PRD">
- <settings>
- <add key ="ck" value ="ckPRD" />
- <add key ="abc" value ="mlPRD" />
- <add key ="cde" value ="p3PRD" />
- <add key ="efg" value ="oppPRD" />
- </settings>
- </server>
- </serverConfiguration>
Fetch the Specific Setting Value
- var server = ServerConfigManager.GetServerSetting("dev","ck");
-
- Console.WriteLine(server);
-
-
- // ckDEV
Fetch the All Setting Value
- var server = ServerConfigManager.GetServer("Dev");
-
- foreach (KeyValuePair<string, string> setting in server?.Settings)
- {
- Console.WriteLine($"Key : {setting.Key} Value : {setting.Value}");
- }
-
-
-
-
-
-
-
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.
-
- 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.