Search code examples
.netapp-configconsole-applicationcustom-sections

Multiple identical custom config sctions in app.config


I'm attempting to create a custom config section in the app.config file of my C# .NET console application. It's to store some details about some servers, like so:

<configSections>
  <sectionGroup name="serverGroup">
    <section name="server" type="RPInstaller.ServerConfig" allowLocation="true" allowDefinition="Everywhere"/>
  </sectionGroup>
</configSections>
<serverGroup>
  <server>
    <name>rmso2srvm</name>
    <isBatchServer>false</isBatchServer>
  </server>
  <server>
    <name>rmsb2srvm</name>
    <isBatchServer>true</isBatchServer>
  </server>
</serverGroup>

I have a class defined for the server section like so:

namespace RPInstaller
{
    public class ServerConfig : ConfigurationSection
    {
        [ConfigurationProperty("name", IsRequired=true)]
        public string Name {...}

        [ConfigurationProperty("isBatchServer", IsRequired = true)]
        public bool IsBatchServer {...}
    }
}

When I now attempt to load the server sections I get an exception: "Sections must only appear once per config file".

How would I be able to legally define multiple server sections within my app.config file?


Solution

  • <confgisections>
        <section name="server" type="RPInstaller.ServerConfig" allowLocation="true" allowDefinition="Everywhere"/>
    </confgisections>
    <server>
      <servers>
        </clear>
        <add name="rmso2srvm" isBatchServer="false"/>
        <add name="rmsb2srvm" isBatchServer="true"/>
      </servers>
    </server>
    

    Is how I have set up a custom section previously

    VB Code to access:

     Dim cfg As ServerSection = (ConfigurationManager.GetSection("Server"),ServerSection)
     cfg.ServersCollection("nameOfServer").isBatchServer