我想在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