Split xml configuration between files

39 views
Skip to first unread message

Павел Аниховский

unread,
Jul 6, 2015, 3:53:54 AM7/6/15
to aut...@googlegroups.com
Hi all!
I have the following app.config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <autofac>
    <files>
      <file name="ContainerConfig/NetworkFTP.xml"   section="Network.FTP"/>
      <file name="ContainerConfig/NetworkSFTP.xml"  section="Network.SFTP"/>
    </files>
  </autofac>
</configuration>

and NetworkFTP.xml file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <!--required section declarations for Autofac SectionHandler -->
  <configSections>
    <section name="Network.FTP"   type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
  </configSections>
 
  <Network.FTP defaultAssembly="Network">
    <components>
    ...//some components
    </components>
  </Network.FTP>
</configuration>

I try to load this configuration with this code:
builder.RegisterModule(new ConfigurationSettingsReader("Network.FTP"));
but I get the following error: "Could not read Network.FTP section" :(

Where is my mistake?
Thanks a lot!

Travis Illig

unread,
Jul 6, 2015, 10:43:33 AM7/6/15
to aut...@googlegroups.com, antipa...@gmail.com
When you register your configuration settings reader module, you're registering it for the main app.config, not for the external configs. Looking in your app.config, there's no section called "Network.FTP" - it's called "autofac."

Also, make sure you include the <configSections> part in the app.config as well or it won't work.

app.config:

<?xml version="1.0" encoding="utf-8" ?>
<configSections>
  <section name="autofac"   type="Autofac.Configuration.SectionHandler, Autofac.Configuration"/>
</configSections>

<configuration>
  <autofac>
    <files>
      <file name="ContainerConfig/NetworkFTP.xml"  section="Network.FTP"/>
      <file name="ContainerConfig/NetworkSFTP.xml" section="Network.SFTP"/>
    </files>
  </autofac>
</configuration>

For registration (since you're using the default configuration section name) you don't have to specify "autofac" as the name:

builder.RegisterModule<ConfigurationSettingsReader>();


Hope that helps. For the future, it's good to post usage questions like this on StackOverflow and tag them autofac so the whole community can benefit and have the chance to answer.
-T
Reply all
Reply to author
Forward
0 new messages