每个配置文件只能出现一次!为什么?

时间:2009-11-20 04:34:42

标签: c# configuration app-config configurationsection

我得到以下例外: “每个配置文件只能出现一次。有关例外,请参阅帮助主题。”

我的配置文件如下所示

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

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

由于

2 个答案:

答案 0 :(得分:14)

您正在尝试将节组用作集合,并将节作为集合中的项目,而不是它们的用途,因此会出错。

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

<强>配置

<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是映射到&lt; point.Services&gt;的配置部分。 section,所以静态Get()方法反映了这个
  • PointServices定义一个集合属性Services,它是一个PointServiceCollection,其中项类型是PointService(NOT PointServices)并映射到元素名称xService
  • PointService元素是元素而不是部分,再次注意,collection属性定义了项类型,而不是容器类型

希望有所帮助。

答案 1 :(得分:-1)

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