S h o r t S t o r i e s

// Tales from software development

.NET configuration settings configSource attribute

leave a comment »

Periodically I need to include a config settings file in another config settings file and I always forget how to do it so I’m blogging about it so I know where to find the information the next time I forget and need to remember.

It’s the configSource attribute that allows you to include an external file in an application Settings config section. Here’s a quick example. The first file is the config file without an external file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Vitality.Import.Processor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Vitality.Import.Processor.Properties.Settings>
            <setting name="ConnectionString" serializeAs="String">
                <value>Database=clinical;Data Source=neptune;User Id=vitality;Password=vitality</value>
            </setting>
        </Vitality.Import.Processor.Properties.Settings>
    </applicationSettings>
</configuration>

 
To move the Vitality.Import.Processor.Properties.Settings to an external file called ‘external.config’ you need to do this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="Vitality.Import.Processor.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <applicationSettings>
        <Vitality.Import.Processor.Properties.Settings configSource="external.config" />
    </applicationSettings>
</configuration>

 
And the external config will have the <Vitality.Import.Processor.Properties.Settings> element as its root:

<?xml version="1.0" encoding="utf-8" ?>
<Vitality.Import.Processor.Properties.Settings>
 <setting name="ConnectionString" serializeAs="String">
  <value>Database=clinical;Data Source=neptune;User Id=vitality;Password=vitality</value>
 </setting>
</Vitality.Import.Processor.Properties.Settings>

 
Note that the configSource attribute cannot be used to mix internal and external settings values, i.e. if the configSource attribute is specified then the *.Settings element cannot contain any child elements and all the settings will be read from the external config file.

Advertisements

Written by Sea Monkey

January 25, 2011 at 8:00 pm

Posted in Development

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: