App.config 中的自定义标签 - 无法识别我的 ConfigurationSection class 类型
Custom tags in App.config - type of my ConfigurationSection class is not recognised
我(几乎)完全按照 MSDN 中的示例在我的 App.config 中创建自定义标签(在此处查找文档:
https://msdn.microsoft.com/en-us/library/2tw134k3.aspx) 但我收到此错误:
An unhandled exception of type
'System.Configuration.ConfigurationErrorsException' occurred in
System.configuration.dll
Additional information: An error occurred creating the configuration
section handler for MyServiceGroup/ServiceUpdater: Could not load type
'MyNamespace.ServiceUpdaterSection' from assembly
'System.configuration, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f33d50a4a'.
错误是在这一行触发的(当我尝试使用来自 App.config 的自定义信息时,在我的控制台应用程序的 Main 内):
MyNamespace.ServiceUpdaterSection serviceUpdaterSection =
(ServiceUpdaterSection)ConfigurationManager.GetSection("MyServiceGroup/ServiceUpdater");
并且从错误消息中我已经可以看出这是因为它试图在 System.Configuration 中找到 MyNamespace.ServiceUpdaterSection,相反,它应该在 class (ServiceUpdaterSection) 中找到MyNamespace,因为我已为其指定完全限定名称。
这是我的 App.config 的样子:
<configSections>
<sectionGroup name="MyServiceGroup">
<section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
</sectionGroup>
</configSections>
在下面 App.config 我有:
<MyServiceGroup>
<ServiceUpdater>
<licenseKey id="blablabla"/>
</ServiceUpdater>
至于 ServiceUpdaterSection class,它看起来如下:
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;
}
}
}
}
请问您对此有何看法?
谢谢。
错误在这里:
<section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
应该是:
<section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection, MyNamespace"/>
如果其他人遇到同样的问题,将保留它。
我(几乎)完全按照 MSDN 中的示例在我的 App.config 中创建自定义标签(在此处查找文档: https://msdn.microsoft.com/en-us/library/2tw134k3.aspx) 但我收到此错误:
An unhandled exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.configuration.dll
Additional information: An error occurred creating the configuration section handler for MyServiceGroup/ServiceUpdater: Could not load type 'MyNamespace.ServiceUpdaterSection' from assembly 'System.configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f33d50a4a'.
错误是在这一行触发的(当我尝试使用来自 App.config 的自定义信息时,在我的控制台应用程序的 Main 内):
MyNamespace.ServiceUpdaterSection serviceUpdaterSection =
(ServiceUpdaterSection)ConfigurationManager.GetSection("MyServiceGroup/ServiceUpdater");
并且从错误消息中我已经可以看出这是因为它试图在 System.Configuration 中找到 MyNamespace.ServiceUpdaterSection,相反,它应该在 class (ServiceUpdaterSection) 中找到MyNamespace,因为我已为其指定完全限定名称。
这是我的 App.config 的样子:
<configSections>
<sectionGroup name="MyServiceGroup">
<section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
</sectionGroup>
</configSections>
在下面 App.config 我有:
<MyServiceGroup>
<ServiceUpdater>
<licenseKey id="blablabla"/>
</ServiceUpdater>
至于 ServiceUpdaterSection class,它看起来如下:
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;
}
}
}
}
请问您对此有何看法?
谢谢。
错误在这里:
<section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection"/>
应该是:
<section name="ServiceUpdater" type="MyNamespace.ServiceUpdaterSection, MyNamespace"/>
如果其他人遇到同样的问题,将保留它。