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

发布于 2024-08-12 01:58:41 字数 2207 浏览 4 评论 0原文

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

这是代码:

    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 添加到部分定义中,如下所示:

但它似乎没有帮助。

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

I'm trying to build a custom configuration and with some reason, I can't get it work. I'll appreciate if anyone can see where my problem is.

Here is the code:

    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();
            }
        }


    }

and here is the config:

     <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>

When I'm running: PointServices.Get(), I'm getting:

Unrecognized element 'xService'.

I tried to add xService to the section definition as following:
<section name="xService" type="XYZPoint.Messaging.PointServiceConfiguration.PointService, Barcap.FIA.Point.Messaging" /> but it didn't seem to help.

If anyone has any idea, please help! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陌伤ぢ 2024-08-19 01:58:41

并且您需要另一个 xService 说明符

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

And you need another specifier for xService

<sectionGroup name="point.Services">          
  <sectionGroup name="xServices">          
    <section name="xService" 
      type="XYZ.Messaging.PointServiceConfiguration.PointServices, XYZ.Point.Messaging" />  
  </sectionGroup name="xServices">         
</sectionGroup>
日裸衫吸 2024-08-19 01:58:41

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

xServices should be a sectionGroup, not a section. And xService should be defined as a section.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文