2011-09-11 11 views
9

私は、.NET 3.5のWindowsサービスのApp.configファイルに次のビットを持っている:私はconfigurationServiceSection.configでこれを持っている認識できない要素例外

<configSections> 
    <section name="ConfigurationServiceSection" type="SomeApp.Framework.Configuration.ConfigurationServiceSection, SomeApp.Framework"/> 
</configSections> 

<ConfigurationServiceSection configSource="ConfigSections\configurationServiceSection.config" /> 

<ConfigurationServiceSection> 
    <ConfigurationServices> 
     <ConfigurationService name="LocalConfig" host="localhost" port="40001" location="LON"/> 
    </ConfigurationServices> 
</ConfigurationServiceSection> 
を私は次のように設定にアクセスしようとすると

using System.Configuration; 

namespace SomeApp.Framework.Configuration 
{ 
    public sealed class ConfigurationServiceSection : ConfigurationSection 
    { 
     [ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)] 
     [ConfigurationCollection(typeof(ConfigurationServices))] 
     public ConfigurationServices ConfigurationServices 
     { 
      get 
      { 
       return (ConfigurationServices)base["ConfigurationServices"]; 
      } 
     } 
    } 

    public sealed class ConfigurationServices : ConfigurationElementCollection 
    { 
     protected override ConfigurationElement CreateNewElement() 
     { 
      return new ConfigurationService(); 
     } 

     protected override object GetElementKey(ConfigurationElement element) 
     { 
      ConfigurationService configService = (ConfigurationService) element; 
      return configService.Name; 
     } 
    } 

    public sealed class ConfigurationService : ConfigurationElement 
    { 
     /// <summary> 
     /// name 
     /// </summary> 
     [ConfigurationProperty("name", IsKey = true, IsRequired = true)] 
     public string Name 
     { 
      get { return (string)this["name"]; } 
      set { this["name"] = value; } 
     } 

     /// <summary> 
     /// host 
     /// </summary> 
     [ConfigurationProperty("host", IsKey = false, IsRequired = true)] 
     public string Host 
     { 
      get { return (string)this["host"]; } 
      set { this["host"] = value; } 
     } 

     /// <summary> 
     /// port 
     /// </summary> 
     [ConfigurationProperty("port", IsKey = false, IsRequired = true)] 
     public string Port 
     { 
      get { return (string)this["port"]; } 
      set { this["port"] = value; } 
     } 

     /// <summary> 
     /// location 
     /// </summary> 
     [ConfigurationProperty("location", IsKey = false, IsRequired = true)] 
     public string Location 
     { 
      get { return (string)this["location"]; } 
      set { this["location"] = value; } 
     } 
    } 
} 

そして、ここではコードです

var configurationServiceSection = (ConfigurationServiceSection)configuration.GetSection("ConfigurationServiceSection"); 

私はこの例外を取得:

Unrecognized element 'ConfigurationService'. (C:\Code\branches\ConfigurationService\SomeApp\Src\ConfigService\SomeApp.ConfigService.WindowsService\bin\Debug\ConfigSections\configurationServiceSection.config line 3) 

すべてが私に順番に見えますか?

どのようなアイデアをお願いしますか?ありがとう。私は以下のとおり、ConfigurationServiceSectionクラスに「AddItemName」を追加

+0

関連項目:[私は解決することができますどのように「認識できない要素 'がelementName'](http://stackoverflow.com/questions/1985047/how-can-i-solve-unrecognized-element -elementname-line-x-line-x)を指定します。 –

答えて

11

OK]をクリックして、このの底になった

public sealed class ConfigurationServiceSection : ConfigurationSection 
{ 
    [ConfigurationProperty("ConfigurationServices", IsDefaultCollection = true, IsRequired = true)] 
    [ConfigurationCollection(typeof(ConfigurationServices), AddItemName = "ConfigurationService")] 
    public ConfigurationServices ConfigurationServices 
    { 
     get 
     { 
      return (ConfigurationServices)base["ConfigurationServices"]; 
     } 
    } 
} 

別の選択肢を通り、CollectionTypeはとのElementNameプロパティを上書きすることでした以下:

public override ConfigurationElementCollectionType CollectionType 
{ 
    get { return ConfigurationElementCollectionType.BasicMap; } 
} 

protected override string ElementName 
{ 
    get { return "ConfigurationService"; } 
} 
+0

私はそれを "return ConfigurationElementCollectionType.BasicMap;"私は "Return ConfigurationElementCollectionType.AddRemoveClearMap;"に変更する必要がありましたこの投稿は私にその答えに手伝ってくれました! – Dib

+0

このAddItemNameソリューションはかなり明白です。かなり直感的です。かなり発見可能。コレクションにアイテムを追加しようとするまで、xml構造体がオブジェクトモデルに似るように構成フレームワークを設計します。オブジェクト型/名前の代わりにxml要素を使用します。それは、それらを推測し続けるでしょう。約6時間。 –

+0

コレクションクラスのElementNameをオーバーライドすることは、これが私のために働くための重要な部分でした。 AddItemNameを設定するだけでは不十分です。言うまでもなく、これはMSDNの例にはありません。 –

関連する問題