【问题标题】:Web.config - Custom configuration to hold a collection inside a collectionWeb.config - 在集合中保存集合的自定义配置
【发布时间】:2013-02-19 08:20:10
【问题描述】:

我正在使用 <configSections></configSections>. 在 web.config 中设置自定义配置

我想要并在 web.config 中创建的标签结构是:

<TwitterCredentialsSectionGroup>
    <TwitterCredentialsSection>
      <accounts>
        <account id="ID1">
          <add key="ConsumerKey" value="..."/>
          <add key="ConsumerSecretKey" value="..."/>
          <add key="AccessToken" value="..."/>
          <add key="AccesstSecretToken" value="..."/>
        </account>
      </accounts>
    </TwitterCredentialsSection>
  </TwitterCredentialsSectionGroup>

我使用以下标签在 configSections 标签内注册了我的自定义配置部分:

<configSections>
<sectionGroup name="TwitterCredentialsSectionGroup">
  <section name="TwitterCredentialsSection" type="TwitterIntegration.TwitterCredentialsSection"
           allowLocation="true" allowDefinition="Everywhere" />
</sectionGroup>

这里有 2 个不同的集合元素,即:

  1. 账号(持有各推特账号密钥)
  2. 帐户(将保存所有 twitter 帐户实例/标签)

现在我的自定义配置代码文件如下:

namespace TwitterIntegration
{
    public class TwitterCredentialsSection : ConfigurationSection
    {

    [ConfigurationProperty("accounts")]
    public AccountCollection Accounts
    {
        get
        {
            return (AccountCollection)this["accounts"];
        }
    }

    [ConfigurationProperty("account")]
    public AccountElement Account
    {
        get
        {
            return (AccountElement)this["account"];
        }
    }
}


public class TwitterKeyElement : ConfigurationElement
{
    [ConfigurationProperty("key",IsKey=true, IsRequired=true)]
    public String Key
    {
        get
        {
            return (String)this["key"];
        }
    }

    [ConfigurationProperty("value")]
    public String Value
    {
        get
        {
            return (String)this["value"];
        }
    }
}


[ConfigurationCollection(typeof(TwitterKeyElement))]
public class AccountElement : ConfigurationElementCollection
{
    [ConfigurationProperty("id", IsRequired = true, IsKey = true)]
    public string ID
    {
        get
        {
            return (string)this["id"];
        }
    }

    protected override ConfigurationElement CreateNewElement()
    {
        return new TwitterKeyElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((TwitterKeyElement)element).Key;
    } 
}


[ConfigurationCollection(typeof(AccountElement))]
public class AccountCollection : ConfigurationElementCollection
{
    protected override ConfigurationElement CreateNewElement()
    {
        return new AccountElement();
    }
    protected override object GetElementKey(ConfigurationElement element)
    {
        return ((AccountElement)element).ID;
    }


}

}

现在,当我尝试使用以下代码读取 web.config 时,出现错误:

TwitterCredentialsSection objtcsection = (ConfigurationManager.GetSection("TwitterCredentialsSectionGroup/TwitterCredentialsSection") as TwitterCredentialsSection);

错误: **解析器错误消息:无法识别元素“帐户”。

Source Error: 


Line 19:     <TwitterCredentialsSection>
Line 20:       <accounts>
Line 21:         <account id="ID1"> 
Line 22:           <add key="ConsumerKey" value="W0SKMPuXzml2CHGsMSoHZA"/>
Line 23:           <add key="ConsumerSecretKey" value="kaAS0CgeQqcTWjTvYsie8Owtl8o6N8hcOclY9bKlu4"/> 
**

谁能建议我的代码有什么问题?

谢谢

【问题讨论】:

    标签: .net web-config custom-configuration


    【解决方案1】:

    您的代码只需要两个小改动:

    1. TwitterCredentialsSection 类中的Accounts 属性添加 ConfigurationCollection 属性,以定义无法识别的“帐户”元素:

      public class TwitterCredentialsSection : ConfigurationSection
      {
         [ConfigurationProperty("accounts")]
         [ConfigurationCollection(typeof(AccountCollection), AddItemName = "account")]
         public AccountCollection Accounts
         {
             get { return (AccountCollection) this["accounts"]; }
         }
      }
      
    2. 删除同一 TwitterCredentialsSection 类中的 Account 属性:

      [ConfigurationProperty("account")]
      public AccountElement Account
      {
          get { return (AccountElement) this["account"]; }
      }
      

    【讨论】:

      猜你喜欢
      • 2011-12-05
      • 2011-11-04
      • 2011-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多