嵌套配置节 app.config

21

我没有找到如何在app.config中访问这样一个嵌套配置部分的任何示例。

  <my.configuration>
    <emailNotification>
      <to value="me@you.com" />
      <from value="he@you.com" />
      <subject value="Subject" />
      <smtpHost value="smtp.you.com" />
      <triggers>
        <add name="1" varAlias="Var1" lower="-200" upper="-150"/>
      </triggers>  
    </emailNotification>
  </my.configuration>

我之前使用过ConfigurationElementCollection和ConfigurationElement。但我不知道如何做到上面所说的?


我猜 my.configuration 是一个组,而 emailNotification 是你所指的嵌套部分。对吗? - user595010
我的配置是一个部分<section name="my.configuration" type="IoLoggingService.MyConfigSection, IoLoggingService"/>,并且emailNotification是该组。 - chriszero
我马上会发布一个例子。 - user595010
1个回答

38
你需要:
my.configuration定义为部分组,将emailNotification定义为组内的一个部分。将以下内容添加到配置文件中:
<configSections>
    <sectionGroup name="my.configuration"
                  type="SectionGroupRetrieval.MyConfigurationGroup, SectionGroupRetrieval">
        <section name="emailNotification"
                 type="SectionGroupRetrieval.EmailNotificationSection, SectionGroupRetrieval" />
    </sectionGroup>       
</configSections>

实现配置部分组(my.configuration)。

public class MyConfigurationGroup : ConfigurationSectionGroup
{
    [ConfigurationProperty( "emailNotification" )]
    public EmailNotificationSection EmailNotification
    {
        get { return (EmailNotificationSection)base.Sections[ "emailNotification" ]; }
    }
}

实现配置部分(emailNotification)。

public class EmailNotificationSection : ConfigurationSection
{
    [ConfigurationProperty( "to" )]
    public ValueElement To
    {
        get { return (ValueElement)base[ "to" ]; }
    }

    [ConfigurationProperty( "from" )]
    public ValueElement From
    {
        get { return (ValueElement)base[ "from" ]; }
    }

    [ConfigurationProperty( "subject" )]
    public ValueElement Subject
    {
        get { return (ValueElement)base[ "subject" ]; }
    }

    [ConfigurationProperty( "smtpHost" )]
    public ValueElement SmtpHost
    {
        get { return (ValueElement)base[ "smtpHost" ]; }
    }

    [ConfigurationProperty( "triggers" )]
    public TriggerElementCollection Triggers
    {
        get { return (TriggerElementCollection)base[ "triggers" ]; }
    }
}

实现必要的配置元素和配置元素集合。

public class ValueElement : ConfigurationElement
{
    [ConfigurationProperty( "value" )]
    public string Value
    {
        get { return (string)base[ "value" ]; }
        set { base[ "value" ] = value; }
    }
}

public class TriggerElement : ConfigurationElement
{
    [ConfigurationProperty( "name" )]
    public string Name
    {
        get { return (string)base[ "name" ]; }
        set { base[ "name" ] = value; }
    }

    [ConfigurationProperty( "varAlias" )]
    public string VarAlias
    {
        get { return (string)base[ "varAlias" ]; }
        set { base[ "varAlias" ] = value; }
    }

    [ConfigurationProperty( "lower" )]
    public int Lower
    {
        get { return (int)base[ "lower" ]; }
        set { base[ "lower" ] = value; }
    }

    [ConfigurationProperty( "upper" )]
    public int Upper
    {
        get { return (int)base[ "upper" ]; }
        set { base[ "upper" ] = value; }
    }
}

[ConfigurationCollection( typeof( TriggerElement ) )]
public class TriggerElementCollection : ConfigurationElementCollection
{
    public TriggerElement this[ string name ]
    {
        get { return (TriggerElement)base.BaseGet( name ); }
    }

    public TriggerElement this[ int index ]
    {
        get { return (TriggerElement)base.BaseGet( index ); }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new TriggerElement();
    }

    protected override object GetElementKey( ConfigurationElement element )
    {
        return ( (TriggerElement)element ).Name;
    }
}

在更新配置文件并实现必要的配置位后,您可以按以下方式访问您的部分:

Configuration config = ConfigurationManager.OpenExeConfiguration( ConfigurationUserLevel.None );
MyConfigurationGroup myConfiguration = (MyConfigurationGroup)config.GetSectionGroup( "my.configuration" );
EmailNotificationSection section = myConfiguration.EmailNotification;

当使用Configuration configuration = WebConfigurationManager.OpenWebConfiguration(HttpContext.Request.ApplicationPath)时,ASP.NET MVC 4.5同样适用。 - Ako

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接