关于C#:自定义app.config节导致配置加载错误

Custom App.config section causing configuration load error

我使用app.config文件中的自定义配置部分,在应用程序中发生错误时,根据jan remunda在此处的文章自动向某些管理员发送电子邮件:如何在app.config中创建自定义配置部分?

App.config:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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="[email protected]" />
    <ErrorRecipients>
      <ErrorRecipient address="[email protected]" />
      <ErrorRecipient address="[email protected]" />
    </ErrorRecipients>
  </ErrorEmailer>
</configuration>

我已经添加了这些配置元素:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
public class ErrorRecipient : ConfigurationElement
{
    [ConfigurationProperty("address", IsRequired = true)]
    public string Address
    {
        get
        {
            return this["address"] as string;
        }
    }
}

public class SmtpServer : ConfigurationElement
{
    [ConfigurationProperty("address", IsRequired = true)]
    public string Address
    {
        get
        {
            return this["address"] as string;
        }
    }
}

public class FromAddress : ConfigurationElement
{
    [ConfigurationProperty("address", IsRequired = true)]
    public string Address
    {
        get
        {
            return this["address"] as string;
        }
    }
}

此配置元素集合:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
    }
}

此配置部分:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
        }
    }
}

但在运行程序并尝试运行此程序时,出现"配置系统未能初始化"错误:

1
2
var config = ErrorEmailer.GetConfig();
Console.WriteLine(config.SmtpServer.Address);

configSections元素必须是configuration元素的第一个子元素。试试这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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="[email protected]" />
    <ErrorRecipients>
      <ErrorRecipient address="[email protected]" />
      <ErrorRecipient address="[email protected]" />
    </ErrorRecipients>
  </ErrorEmailer>
</configuration>

此外,还需要将自定义配置节类型的名称空间和程序集放入section元素中。拿这个:

1
<section name="ErrorEmailer" type="EmailTester.ErrorEmailer" />

然后像这样添加程序集:

1
2
<section name="ErrorEmailer"
         type="EmailTester.ErrorEmailer, My.Containing.Assembly.Goes.Here" />

如果不使用包含在.NET中的程序集限定配置类型的命名空间路径,.NET假定您是指System.Configuration程序集。