部分只能对每个配置文件出现一次!为什么? [英] Sections must only appear once per config file! why?
问题描述
我得到以下exeption:
部分只能出现一次配置文件。请参阅帮助主题的例外。
I'm getting the following exeption: "Sections must only appear once per config file. See the help topic for exceptions. "
我的配置文件看起来像这样:
<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"]; }
}
}
如果您有任何想法
感谢
推荐答案
正在尝试使用一个部分组作为集合,而部分作为集合中的项目,这不是他们打算的,因此错误。
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.
基本上你需要定义point.Services作为一个节,因为它不需要包含任何其他节,然后定义一个集合属性来包含配置元素。您可以按以下方式更新代码:
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:
配置:
<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"]; }
}
}
>:
To break it down:
- PointServices是映射到< point.Services>节的配置节,因此静态Get / li>
- PointServices定义一个集合属性Services,它是一个PointServiceCollection,其中项类型为PointService(NOT PointServices),并映射到元素名xService
- 元素是元素而不是节,请注意,collection属性定义项类型,而不是容器类型
希望有帮助。
这篇关于部分只能对每个配置文件出现一次!为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!