How to have a common ConfigSource file for ConfigSections and connectionStrings?

1.1k Views Asked by At

My project has 2 different config sections(one technical & one functional) and some connection Strings. I would like to have in a same configSource file, the technical config section & the connection strings & in an other one the functional section. I know how to do this in 3 separate files but not in 2. It would be logical to have technical configuration like server hostnames & connection string in the same file.

My configuration files should look like this:

App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="MyService.Functional" type="Logger.ConfigHandler, Logger"/>
    <section name="MyService.Technical" type="Logger.ConfigHandler, Logger"/>
  </configSections>

  <MyService.Functional configSource="Config\MyService.Functional.Config"/>
  <MyService.Technical configSource="Config\MyService.Technical.Config"/>

  <connectionStrings configSource="Config\MyService.Technical.Config">
  </connectionStrings>
</configuration>

MyService.Technical.Config

<MyService.Technical.Config>
  <MyResourceServer value="tcp://MyServer:9000"/>
 </MyService.Technical.Config>

 <connectionStrings>
   <add name="MyEntities" [...] />
 </connectionStrings>

However if I mix the section MyService.Technical & the connectionStrings in the same file, the ConfigurationManager can't load any section anymore.

Do you have any tip to do this ? Is it absolutely mandatory to have 3 separate files for this case ?

1

There are 1 best solutions below

0
On

From my experience, it seems to load the contents of the file as the inner XML of the referring element, which would mean you need to have different files for each section being externally referenced.