2015-10-19 33 views
1

我想在App.config創建自定義欄目,遇到以下情況除外:定製App.Config中科 - 無法識別的元素異常

ConfigurationErrorsException

無法識別的元素「EncryptedUserCredential 」。 (C:\我的文檔\ Hachette.CRM \ test_app_appsettings \ BIN \調試\ test_app_appsettings.vshost.exe.Config第11行)

現在我在一個完全喪失。我已經進入了RegisterEncryptedUserCredentialsConfig.GetConfig(),並發現該部分爲空的RegisterEncryptedUserCredentialsConfig.EncryptedUserCredentials。我也在網上調查時檢查了所有常見的嫌疑人,例如:

  1. 確定類型在聲明自定義部分時在app.config configSection中包含程序集名稱。
  2. 位於app.config的開頭。

自定義部分出自於:

  1. 類庫
  2. C#/。NET 4.0。

我很茫然,覺得我一直盯着它太久了,週末過去了,需要一些新鮮的眼睛!

爲了方便起見,我添加了C#類庫here中的所有代碼。

這裏是在app.config:

<?xml version="1.0"?> 
<configuration> 
    <configSections> 
    <section name="EncryptedUserCredentials" 
      type="Hachette.Common.CustomConfigSections.RegisterEncryptedUserCredentialsConfig, Hachette.Common"/> 
    </configSections> 
    <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
    <EncryptedUserCredentials> 
    <EncryptedUserCredential userName="garethB" password="[email protected]"/> 
    <EncryptedUserCredential userName="webService" password="[email protected]"/> 
    </EncryptedUserCredentials> 
</configuration> 

這裏是EncryptedUserCredential 的ConfigurationElement

public class EncryptedUserCredential : ConfigurationElement 
{ 
    [ConfigurationProperty("userName", IsRequired = true)] 
    public string UserName 
    { 
     get 
     { 
      return this["userName"] as string; 
     } 
    } 

    [ConfigurationProperty("password", IsRequired = true)] 
    public string Password 
    { 
     get 
     { 
      return this["password"] as string; 
     } 
    } 
} 

這裏是EncryptedCredentials ConfigurationElementCollection中

public class EncryptedUserCredentials : ConfigurationElementCollection 
{ 
    public EncryptedUserCredential this[int index] 
    { 
     get 
     { 
      return base.BaseGet(index) as EncryptedUserCredential; 
     } 
     set 
     { 
      if (base.BaseGet(index) != null) 
      { 
       base.BaseRemoveAt(index); 
      } 
      this.BaseAdd(index, value); 
     } 
    } 

    public new EncryptedUserCredential this[string responseString] 
    { 
     get 
     { 
      return (EncryptedUserCredential)BaseGet(responseString); 
     } 
     set 
     { 
      if (BaseGet(responseString) != null) 
      { 
       BaseRemoveAt(BaseIndexOf(BaseGet(responseString))); 
      } 
      BaseAdd(value); 
     } 
    } 

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

    protected override object GetElementKey(ConfigurationElement element) 
    { 
     return ((EncryptedUserCredential)element).UserName; 
    } 
} 

這裏是RegisterEncryptedUserCredentialsConfig 的ConfigurationSection

public class RegisterEncryptedUserCredentialsConfig : ConfigurationSection 
{ 
    public static RegisterEncryptedUserCredentialsConfig GetConfig() 
    { 
     //var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); 

     return (RegisterEncryptedUserCredentialsConfig)System.Configuration.ConfigurationManager.GetSection("EncryptedUserCredentials") ?? new RegisterEncryptedUserCredentialsConfig(); 
    } 

    [System.Configuration.ConfigurationProperty("EncryptedUserCredentials", IsDefaultCollection=true,IsRequired=true)] 
    [ConfigurationCollection(typeof(EncryptedUserCredentials), AddItemName="EncryptedUserCredential")] 
    public EncryptedUserCredentials EncryptedUserCredentials 
    { 
     get 
     { 
      object o = this["EncryptedUserCredentials"]; 
      return o as EncryptedUserCredentials; 
     } 


    } 

} 

上述所有住在命名空間:

namespace Hachette.Common.CustomConfigSections 

並在以下大會:

enter image description here

+1

我認爲這個問題是你的'ConfigurationSection'不是'ConfigurationElementCollection' – DavidG

+0

@DavidG:首先感謝您的回覆迅速,讚賞。你的意思是我在哪裏使用了屬性'[ConfigurationCollection(typeof ..'? – garfbradaz

+0

@DavidG:聽起來不錯。只是爲了確認,你的意思是: http://chat.stackoverflow.com/ – garfbradaz

回答

1

這是不可能的爲根元素作爲集合持有者。所以,你應該編寫您的配置,以匹配該結構(請注意我的根元素起名字,以符合您的命名空間,但隨時可以選擇任何你喜歡):

<hachette> 
    <EncryptedUserCredentials> 
    <EncryptedUserCredential userName="garethB" password="[email protected]"/> 
    <EncryptedUserCredential userName="webService" password="[email protected]"/> 
    </EncryptedUserCredentials> 
</hachette> 

這意味着你的配置層次結構將具有根ConfigSection,其依次包含ConfigurationElementCollection,其包含所有ConfigurationElement對象。

這裏是你如何可以寫一個例子文章:http://www.abhisheksur.com/2011/09/writing-custom-configurationsection-to.html

+0

此答案的第一個陳述是錯誤的。根元素可以很容易地成爲集合持有者。額外的級別是不必要的。請參閱https://stackoverflow.com/a/14782024/426379 – Saul

相關問題