First solution:
In web.config file in the section "appSettings" available attribute "file" for moving segment of web config to another file. It's nice but we need more then one config file and therefore required additional configurations.Create "sectionGroup":
- <configuration>
- <configSections>
- <sectionGroup name="Plugins">
- <section name="Module1" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- <section name="Module2" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- </sectionGroup>
- </configSections>
- .....
Module1 and Module2 this is a names of our plugins. Create configuration section for group and define key for path of module config file in every named section:
- <Plugins>
- <Module1>
- <add key="ConfigFile" value="App1.config"/>
- </Module1>
- <Module2>
- <add key="ConfigFile" value="App2.config"/>
- </Module2>
- </Plugins>
Create 2 new config files "App1.config" and "App2.config". In the properties of files change "Copy to Output Directory" to "Copy always". The content of module config file look like this one:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="AnyKey" value="Bla-Bla-Bla"/>
- <add key="AnyKey2" value="Bla-Bla-Bla"/>
- </appSettings>
- </configuration>
An using code:
- class Test
- {
- private static readonly Configuration config = ConfigurationManager.OpenMappedExeConfiguration(
- //Get config file
- new ExeConfigurationFileMap()
- { //Web.config part //File name
- ExeConfigFilename = ((NameValueCollection)ConfigurationManager.GetSection("Plugins/Module1"))["ConfigFile"]
- },
- ConfigurationUserLevel.None);
- private void Foo()
- {
- var value = config.AppSettings.Settings["AnyKey"].Value;
- Console.WriteLine(value);
- }
- }
Second solution:
Add new section "Plugin". We are use the type DictionarySectionHandler in order to get values as Dictionary:
- <configuration>
- <configSections>
- <section name="Plugin" type="System.Configuration.DictionarySectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
- </configSections>
Add values:
- <Plugin>
- <add key="ConfigFileOfModule1" value="App1.config"/>
- <add key="ConfigFileOfModule2" value="App2.config"/>
- </Plugin>
Create 2 new config files "App1.config" and "App2.config". In the properties of files change "Copy to Output Directory" to "Copy always". The content of module config file look like this one:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="AnyKey" value="Bla-Bla-Bla"/>
- <add key="AnyKey2" value="Bla-Bla-Bla"/>
- </appSettings>
- </configuration>
An using code:
- private static readonly IDictionary section = (IDictionary)ConfigurationManager.GetSection("Plugin");
- private static readonly System.Configuration.Configuration config =
- ConfigurationManager.OpenMappedExeConfiguration(
- new ExeConfigurationFileMap()
- {
- ExeConfigFilename = section["ConfigFileOfModule1"].ToString()
- }, ConfigurationUserLevel.None);
- private void Foo()
- {
- var value = config.AppSettings.Settings["AnyKey"].Value;
- }
Enjoy!
No comments:
Post a Comment