【问题标题】:Sections must only appear once per config file! why?每个配置文件中的部分只能出现一次!为什么?
【发布时间】:2014-06-19 00:41:42
【问题描述】:

我收到以下异常: "每个配置文件的部分只能出现一次。有关例外情况,请参阅帮助主题。"

我的配置文件如下所示

<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>

这是我加载它的代码

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"]; }
    }
}

如果您知道我为什么会收到此错误,那将很有帮助。

谢谢

【问题讨论】:

    标签: c# configuration app-config configurationsection


    【解决方案1】:

    您正在尝试将部分组用作集合,并将部分用作集合中的项目,这不是它们的用途,因此会出现错误。

    基本上你只需要将point.Services定义为一个section,因为它不需要包含任何其他section,然后定义一个collection属性来包含配置元素。您可以按如下方式更新代码:

    配置

    <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>
    

    那么代码是

    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"]; }
        }
    }
    

    分解

    • PointServices 是映射到 部分的配置部分,因此静态 Get() 方法反映了这一点
    • PointServices 定义了一个集合属性 Services,它是一个 PointServiceCollection,其中项类型是 PointService(不是 PointServices),并且映射到元素名称 xService
    • PointService 元素是元素而不是部分,再次注意集合属性属性定义项目类型,而不是容器类型

    希望对您有所帮助。

    【讨论】:

    • 出色的解决方案,正是我所需要的。我只会添加 PointServicesCollection 类需要从 ConfigurationElementCollection 继承的事实。 This post 也有帮助。
    【解决方案2】:

    Point.System 在配置文件中的什么位置?它可能只是在抱怨,因为 0 != 1(对计算机)

    【讨论】:

    • 不,point.system 与它没有任何关系。这部分工作正常。它与point.services有关
    猜你喜欢
    • 2014-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-02
    相关资源
    最近更新 更多