【问题标题】:Custom tags in App.config - type of my ConfigurationSection class is not recognisedApp.config 中的自定义标签 - 我的 ConfigurationSection 类的类型无法识别
【发布时间】:2017-12-04 18:06:00
【问题描述】:

我已经(几乎)按照 MSDN 中的示例在我的 App.config 中创建自定义标签(在此处查找文档: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx) 但我收到此错误:

未处理的类型异常 'System.Configuration.ConfigurationErrorsException' 发生在 System.configuration.dll

附加信息:创建配置时出错 MyServiceGroup/ServiceUpdater 的部分处理程序:无法加载类型 程序集中的“MyNamespace.ServiceUpdaterSection” 'System.configuration,版本=4.0.0.0,文化=中性, PublicKeyToken=b03f5f7f33d50a4a'。

并且在此行上触发了错误(当我尝试使用来自 App.config 的自定义信息时,在我的控制台应用程序的 Main 内部):

MyNamespace.ServiceUpdaterSection serviceUpdaterSection =
(ServiceUpdaterSection)ConfigurationManager.GetSection("MyServiceGroup/ServiceUpdater");

从错误消息中我已经可以看到这是因为它试图在 System.Configuration 中找到 MyNamespace.ServiceUpdaterSection,相反,它应该在 MyNamespace 中找到此类 (ServiceUpdaterSection),因为我已经给了它完全限定的名称.

这是我的 App.config 的外观:

<configSections>
    <sectionGroup name="MyServiceGroup">
      <section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
    </sectionGroup>
    </configSections>

在 App.config 的下面我有:

<MyServiceGroup>
    <ServiceUpdater>
      <licenseKey id="blablabla"/>
    </ServiceUpdater>     

ServiceUpdaterSection 类如下所示:

namespace MyNamespace
{
    public class LicenseKeyElement : ConfigurationElement
    {
        [ConfigurationProperty("id")]
        public string Id
        {
            get
            {
                return (string)this["id"];
            }
            set
            {
                this["id"] = value;
            }
        }
    }
    public class ServiceUpdaterSection : ConfigurationSection
    {
        [ConfigurationProperty("licenseKey")]
        public LicenseKeyElement LicenseKey
        {
            get
            {
                return (LicenseKeyElement)this["licenseKey"];
            }
            set
            {
                this["licenseKey"] = value;
            }
        }
    }

}

请问您对此有何看法?

谢谢。

【问题讨论】:

    标签: asp.net configuration custom-tag


    【解决方案1】:

    错误就在这里:

     <section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
    

    应该是:

     <section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection, MyNamespace"/>
    

    如果其他人遇到同样的问题,会留下它。

    【讨论】:

      猜你喜欢
      • 2019-05-20
      • 2017-05-01
      • 2019-07-10
      • 2023-03-11
      • 1970-01-01
      • 2020-10-20
      • 2022-12-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多