Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sections must only appear once per config file! why?

I'm getting the following exeption: "Sections must only appear once per config file. See the help topic for exceptions. "

my configuration file look like this:

<configSections>
    <sectionGroup name="point.System">
        <section name="singleInstanceCache"
            type="xyz.Point.System.Configuration.SingleInstanceCache, Point.System" />
    </sectionGroup>
    <sectionGroup name="point.Services">
        <sectionGroup name="xServices" type="xyz.Point.Messaging.PointServiceConfiguration.PointServices, Barcap.FIA.Point.Messaging">
            <section name="xService"
                type="xyz.Point.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" />
        </sectionGroup>
    </sectionGroup>
</configSections>

<point.Services>
    <xServices>
        <xService name="Service1" type="IService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
        <xService name="BlobService" type="IPortfolioService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
    </xServices>
</point.Services>

here is the code where i load it:

public class PointServices : ConfigurationSection
{
    public static PointServices Get()
    {
        var t = (PointServices)ConfigurationManager.GetSection("point.Services/xServices");

        return null;
    }

    //<summary>
    //Declares a collection element represented in the following configuration sub-section
    //<singleInstances> <add .../> </singleInstances> 
    //</summary>
    [ConfigurationProperty("xServices", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(PointServices), AddItemName = "xService")]
    public PointServicesCollection Services
    {
        get { return (PointServicesCollection) base["xServices"]; }
    }
}

public class PointService : ConfigurationElement
{
    [ConfigurationProperty("name",IsRequired = true)]
    public string Name
    {
        get { return this["name"].ToString(); }
    }

    [ConfigurationProperty("type", IsRequired = true)]
    public string Type
    {
        get { return this["type"].ToString(); }
    }

    [ConfigurationProperty("endpoints", IsRequired = false)]
    [ConfigurationCollection(typeof(EndpointAliasCollection), AddItemName = "endpoint")]
    public EndpointAliasCollection Endpoints
    {
        get { return (EndpointAliasCollection)this["endpoints"]; }
    }
}

if you have any idea why i'm getting this error, that would be helpful.

Thanks

like image 961
Or A Avatar asked Nov 20 '09 04:11

Or A


People also ask

What is section in web config?

A section handler is a . NET Framework class that implements the ConfigurationSection interface. The declaration area identifies the namespace and class name of each section handler class. Section handlers are used to read and set the settings that pertain to a section.

How do I add a section in app config?

We will start with creating an class that can store the instance settings (the <add> element), then we'll create a collection that can store our instances (the <instances> element) and then we'll create the class to manage the Configuration Section (the <sageCRM> element).

What is the appSettings section in the web config file?

The <appSettings> element stores custom application configuration information, such as database connection strings, file paths, XML Web service URLs, or any other custom configuration information for an application.


1 Answers

You are trying to use a section group as a collection, and sections as items in the collection, which is not what they are intended for, hence the error.

Basically you only need to define point.Services as a section, as it does not need to contain any other sections, then define a collection property to contain configuration elements. You can update the code as follows:

Config:

<configSections>
    <section name="point.Services" 
        type="xyz.Point.Messaging.PointServiceConfiguration.PointServices, Barcap.FIA.Point.Messaging" />
</configSections>

<point.Services>
    <xServices>
        <xService name="Service1" type="IService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
        <xService name="BlobService" type="IPortfolioService" >
            <endpoints>
                <endpoint aliasName="incoming" endpointName="Subscriber"/>
                <endpoint aliasName="outgoing" endpointName="Publisher"/>
            </endpoints>
        </xService>
    </xServices>
</point.Services>

Then the code is:

public class PointServices : ConfigurationSection
{
    public static PointServices Get()
    {
        return (PointServices) ConfigurationManager.GetSection("point.Services");
    }

    [ConfigurationProperty("xServices", IsDefaultCollection = true)]
    [ConfigurationCollection(typeof(PointService), AddItemName = "xService")]
    public PointServicesCollection Services
    {
        get { return (PointServicesCollection) base["xServices"]; }
    }
}

public class PointService : ConfigurationElement
{
    [ConfigurationProperty("name", IsRequired = true)]
    public string Name
    {
        get { return this["name"].ToString(); }
    }

    [ConfigurationProperty("type", IsRequired = true)]
    public string Type
    {
        get { return this["type"].ToString(); }
    }

    [ConfigurationProperty("endpoints", IsRequired = false)]
    [ConfigurationCollection(typeof(EndpointAlias), AddItemName = "endpoint")]
    public EndpointAliasCollection Endpoints
    {
        get { return (EndpointAliasCollection) this["endpoints"]; }
    }
}

To break it down:

  • PointServices is a configuration section that maps to <point.Services> section, so the static Get() method reflects this
  • PointServices defines a collection property Services which is a PointServiceCollection where the item type is PointService (NOT PointServices) and is mapped to the element name xService
  • PointService elements are elements and not sections, again note that the collection property attribute defines the item type, not the container type

Hope that helps.

like image 83
Sam Avatar answered Oct 14 '22 12:10

Sam