【发布时间】: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