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

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

我的配置文件如下所示

  

这是我加载它的代码

 public class PointServices : ConfigurationSection { public static PointServices Get() { var t = (PointServices)ConfigurationManager.GetSection("point.Services/xServices"); return null; } // //Declares a collection element represented in the following configuration sub-section //   // [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"]; } } } 

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

谢谢

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

基本上你只需要将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(NOT PointServices),并映射到元素名称xService
  • PointService元素是元素而不是部分,再次注意collection属性定义项类型,而不是容器类型

希望有所帮助。

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