每个配置文件中的部分只能出现一次!为什么? [英] 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. "
我的配置文件如下:
<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"]; }
}
}
如果你知道为什么我会收到这个错误,那会很有帮助.
if you have any idea why i'm getting this error, that would be helpful.
谢谢
推荐答案
您试图将节组用作集合,并将节用作集合中的项目,这不是它们的用途,因此出现错误.
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"]; }
}
}
分解:
- PointServices 是映射到
部分的配置部分,因此静态 Get() 方法反映了这一点 - PointServices 定义了一个集合属性 Services,它是一个 PointServiceCollection,其中项目类型是 PointService(不是 PointServices)并映射到元素名称 xService
- PointService 元素是元素而不是节,再次注意集合属性属性定义了项目类型,而不是容器类型
希望有所帮助.
这篇关于每个配置文件中的部分只能出现一次!为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!