1

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

我想要并在 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. 帐户(持有每个 Twitter 帐户密钥)
  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"/> 
**

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

谢谢

4

1 回答 1

0

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

  1. 将ConfigurationCollection 属性添加到类中的属性AccountsTwitterCredentialsSection定义无法识别的“帐户”元素:

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

    [ConfigurationProperty("account")]
    public AccountElement Account
    {
        get { return (AccountElement) this["account"]; }
    }
    
于 2014-05-22T15:47:46.223 回答