【问题标题】:ConfigurationSection - Custom Section is undefined - Why?ConfigurationSection - 自定义部分未定义 - 为什么?
【发布时间】:2010-12-18 12:43:25
【问题描述】:

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

代码如下:

    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 添加到部分定义中,如下所示: &lt;section name="xService" type="XYZPoint.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" /&gt; 但它似乎没有帮助。

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

【问题讨论】:

    标签: .net configuration configurationsection


    【解决方案1】:

    你需要另一个 xService 说明符

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

    【讨论】:

    【解决方案2】:

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

    【讨论】:

    猜你喜欢
    • 2013-01-22
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    • 2014-02-01
    • 2020-05-12
    • 2021-07-22
    • 2021-07-18
    • 2012-06-21
    相关资源
    最近更新 更多