4

アプリケーションでエラーが発生したときに、app.config ファイルでカスタム構成セクションを使用して、特定の管理者に自動的に電子メールを送信しています

App.config:

<?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 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;
        }
    }
}

この構成要素コレクション:

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);
4

1 に答える 1

11

要素は、要素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。これを取る:

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

次のようにアセンブリを追加します。

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

構成型の名前空間パスをそれが含まれているアセンブリで修飾しない場合、.NET はSystem.Configurationアセンブリを意味すると見なします。

于 2013-09-03T17:50:24.297 に答える