1

を使用して 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

コードに必要な変更は 2 つだけです。

  1. 認識されない「アカウント」要素を定義するために、ConfigurationCollection 属性をクラスのAccountsプロパティに追加します。TwitterCredentialsSection

    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 に答える