Monday 8 December 2008

Put config sections into a separate file (.Net)

Question:
How do I put DotNet config sections into separate file?



Answer:
As you add config sections to the configuration file of a .Net application the file becomes longer and longer and you start to search your information.
That is the moment where you want to start putting sections into separate files.
It is easy to do and here is how to do it for a SubSonic section of my application.

1. You need a section declaration.
<configSections>
...
<section name="SubSonicService" type="SubSonic.SubSonicSection, SubSonic"
requirePermission="false" restartOnExternalChanges="false"/>
</configSections>

2. The section is pointing to a file.
It uses a relative path to the root of the application.
<SubSonicService configSource="Config\Subsonic.config"/>

3. The file contains the config node.
<SubSonicService defaultProvider="MyProvider">
<providers>
<clear/>
<add name="MyProvider" type="SubSonic.SqlDataProvider, SubSonic"
connectionStringName="MyName" generatedNamespace="MyNamespace"/>
</providers>
</SubSonicService>

No comments: