2015-10-19 120 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

相关问题