ConfigurationSection - 自定义部分未定义 - 为什么?

时间:2009-11-19 22:25:41

标签: .net configuration configurationsection

我正在尝试构建自定义配置,但出于某种原因,我无法让它工作。如果有人能看到我的问题在哪里,我将不胜感激。

以下是代码:

    public class PointServices : ConfigurationSection
    {
        public static PointServices Get()
        {
            var t = ConfigurationManager.GetSection("point.Services/xServices") as PointServices;

            return t;
        }

        //<summary>
         //Declares a collection element represented in the following configuration sub-section
         //<singleInstances> <add .../> </singleInstances> 
         //</summary>
        [ConfigurationProperty("xServices", IsDefaultCollection = true)]
        [ConfigurationCollection(typeof(PointServices))]
        public PointServicesCollection Services
        {
            get
            {
                //var v = base["xServices"];
                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();
            }
        }


    }

这是配置:

     <sectionGroup name="point.Services">
          <section name="xServices" type="XYZ.Messaging.PointServiceConfiguration.PointServices, XYZ.Point.Messaging" />
        </sectionGroup>
...
    <point.Services>
        <xServices>
          <xService>
            <add name="XYZService" type="XYZService" />
          </xService>
        </xServices>
      </point.Services>

当我跑步时:PointServices.Get(),我得到了:

  

无法识别的元素'xService'。

我尝试将xService添加到节定义中,如下所示: <section name="xService" type="XYZPoint.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" />但它似乎没有帮助。

如果有人有任何想法,请帮忙!感谢

2 个答案:

答案 0 :(得分:1)

你需要另一个xService的说明符

<sectionGroup name="point.Services">          
  <sectionGroup name="xServices">          
    <section name="xService" 
      type="XYZ.Messaging.PointServiceConfiguration.PointServices, XYZ.Point.Messaging" />  
  </sectionGroup name="xServices">         
</sectionGroup>

答案 1 :(得分:0)

xServices应该是sectionGroup,而不是section。 xService应定义为一个部分。