C# 导致配置加载错误的自定义App.config节
我正在使用app.config文件中的自定义配置部分,根据Jan Remunda在此处的帖子,在应用程序出现错误时自动向某些管理员发送电子邮件: App.config:C# 导致配置加载错误的自定义App.config节,c#,.net,app-config,C#,.net,App Config,我正在使用app.config文件中的自定义配置部分,根据Jan Remunda在此处的帖子,在应用程序出现错误时自动向某些管理员发送电子邮件: App.config: <?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<configSections>
<section name="ErrorEmailer" type="EmailTester.ErrorEmailer" />
</configSections>
<ErrorEmailer>
<SmtpServer address="mail.smtphost.com" />
<FromAddress address="from@host.com" />
<ErrorRecipients>
<ErrorRecipient address="example@example.com" />
<ErrorRecipient address="example@example.com" />
</ErrorRecipients>
</ErrorEmailer>
</configuration>
此配置元素集合:
public class ErrorRecipients : ConfigurationElementCollection
{
public ErrorRecipient this[int index]
{
get
{
return base.BaseGet(index) as ErrorRecipient;
}
set
{
if (base.BaseGet(index) != null)
{
base.BaseRemoveAt(index);
}
this.BaseAdd(index, value);
}
}
public new ErrorRecipient this[string responseString]
{
get { return (ErrorRecipient)BaseGet(responseString); }
set
{
if (BaseGet(responseString) != null)
{
BaseRemoveAt(BaseIndexOf(BaseGet(responseString)));
}
BaseAdd(value);
}
}
protected override ConfigurationElement CreateNewElement()
{
return new ErrorRecipient();
}
protected override object GetElementKey(ConfigurationElement element)
{
return ((ErrorRecipient)element).Address;
}
}
以及此配置部分:
public class ErrorEmailer : ConfigurationSection
{
public static ErrorEmailer GetConfig()
{
return (ErrorEmailer)System.Configuration.ConfigurationManager.GetSection("ErrorEmailer") ?? new ErrorEmailer();
}
[ConfigurationProperty("ErrorRecipients")]
[ConfigurationCollection(typeof(ErrorRecipients), AddItemName = "ErrorRecipient")]
public ErrorRecipients ErrorRecipients
{
get
{
object o = this["ErrorRecipients"];
return o as ErrorRecipients;
}
}
[ConfigurationProperty("SmtpServer")]
public SmtpServer SmtpServer
{
get
{
object o = this["SmtpServer"];
return o as SmtpServer;
}
}
[ConfigurationProperty("FromAddress")]
public FromAddress FromAddress
{
get
{
object o = this["FromAddress"];
return o as FromAddress;
}
}
}
但在运行程序并尝试运行此程序时,出现“配置系统初始化失败”错误:
var config = ErrorEmailer.GetConfig();
Console.WriteLine(config.SmtpServer.Address);
configSections
元素必须是configuration
元素的第一个子元素。试试这个:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ErrorEmailer" type="EmailTester.ErrorEmailer" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<ErrorEmailer>
<SmtpServer address="mail.smtphost.com" />
<FromAddress address="from@host.com" />
<ErrorRecipients>
<ErrorRecipient address="example@example.com" />
<ErrorRecipient address="example@example.com" />
</ErrorRecipients>
</ErrorEmailer>
</configuration>
并按如下方式添加程序集:
<section name="ErrorEmailer"
type="EmailTester.ErrorEmailer, My.Containing.Assembly.Goes.Here" />
如果您不使用.NET中包含的程序集限定配置类型的命名空间路径,则假定您指的是
System.Configuration
assembly。谢谢,这消除了该问题,但现在我得到了创建ErrorEmailer的配置节处理程序时出错:无法从程序集“System.configuration,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“EmailTester.ErrorEmailer”。“格式应为Namespace.ClassName,类型正确吗?非常感谢您快速、简洁的响应,修复了所有问题。”。将尽快接受。ErrorEmailer是否必须在启动元素中?
<section name="ErrorEmailer" type="EmailTester.ErrorEmailer" />
<section name="ErrorEmailer"
type="EmailTester.ErrorEmailer, My.Containing.Assembly.Goes.Here" />