我想在App.config創建自定義欄目,遇到以下情況除外:定製App.Config中科 - 無法識別的元素異常
ConfigurationErrorsException
無法識別的元素「EncryptedUserCredential 」。 (C:\我的文檔\ Hachette.CRM \ test_app_appsettings \ BIN \調試\ test_app_appsettings.vshost.exe.Config第11行)
現在我在一個完全喪失。我已經進入了RegisterEncryptedUserCredentialsConfig.GetConfig(),並發現該部分爲空的RegisterEncryptedUserCredentialsConfig.EncryptedUserCredentials。我也在網上調查時檢查了所有常見的嫌疑人,例如:
- 確定類型在聲明自定義部分時在app.config configSection中包含程序集名稱。
- 位於app.config的開頭。
自定義部分出自於:
- 類庫
- 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
並在以下大會:
我認爲這個問題是你的'ConfigurationSection'不是'ConfigurationElementCollection' – DavidG
@DavidG:首先感謝您的回覆迅速,讚賞。你的意思是我在哪裏使用了屬性'[ConfigurationCollection(typeof ..'? – garfbradaz
@DavidG:聽起來不錯。只是爲了確認,你的意思是: http://chat.stackoverflow.com/ – garfbradaz