Friday, September 7, 2007

External settings file for ClientSettingsSection (applicationSettings)

Very often we define application settings in application configuration sections and would like to isolate the configuration section into an external settings file. Creating external settings file using the configSource attribute for the predefined configuration sections (AppSettings, ConnectionStrings etc) is well documented. But the literature for custom configuration section is sparse.

Listed below is a sample configuration file that contains a configSource attribute being used in a ClientSettingsSection. I have kept only the essential parts of the file:

   1: <configSections>
   2:   <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup">
   3:     <section name="MyApplication.Properties.Settings" type="System.Configuration.ClientSettingsSection"/>
   4:   </sectionGroup>
   5: </configSections>
   6:  
   7: <applicationSettings>
   8:   <MyApplication.Properties.Settings configSource="BusinessLogic.config" />
   9: </applicationSettings>

Line 8: Using the configSource attributed to define an external settings file called "BusinessLogic.config"


A couple of things of note:
- Unlike how it does for predefined configuration sections, Visual Studio does not provide intellisense when trying to add the configSource attribute to ClientSettingsSection.
- The external settings file has to be in the same directory or subdirectory as the configuration file. So line 8 can be replaced with the following line:


<MyApplication.Properties.Settings configSource="ConfigFiles\BusinessLogic.config" />

Listed below is the "BusinessLogic.config" file


   1: <MyApplication.Properties.Settings>
   2:   <setting name="InitializationVector" serializeAs="String">
   3:     <value>1234567890abc!@#</value>
   4:   </setting>
   5:   <setting name="KeySize" serializeAs="String">
   6:     <value>256</value> 
   7:   </setting>
   8: </MyApplication.Properties.Settings>

 

Technorati Tags: , ,

No comments:

Post a Comment

Subscribe to my feed in your favorite feed reader