Sections must only appear once per config file! why?

社会主义新天地 提交于 2019-12-03 16:09:59
Sam

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.

Where is Point.System in the config file? It might just be complaining because 0 != 1 (to the computer)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!