自定义 App.Config 部分 - 无法识别的元素异常

Custom App.Config Section - Unrecognized element Exception

我正在尝试在 app.config 和 运行 中创建自定义部分,出现以下异常:

ConfigurationErrorsException

Unrecognized element 'EncryptedUserCredential'. (C:\My Documents\Hachette.CRM\test_app_appsettings\bin\Debug\test_app_appsettings.vshost.exe.Config line 11)

现在我完全不知所措了。我进入了 RegisterEncryptedUserCredentialsConfig.GetConfig(),发现 属性 RegisterEncryptedUserCredentialsConfig.EncryptedUserCredentials 中的 Section 为空。我还检查了我在网上调查时的所有常见嫌疑人,例如:

  1. 确保类型在声明自定义部分时在 app.config configSection 中包含程序集名称。
  2. 在 app.config.
  3. 的开头

自定义部分写在:

  1. Class图书馆
  2. C#/.NET 4.0.

我不知所措,觉得周末过去了我盯着它看得太久了,需要一些新鲜的眼睛!

为方便起见,我添加了 C# class 库 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="1235@"/>
    <EncryptedUserCredential userName="webService" password="123ffff5@"/>
  </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

并在以下大会中:

根元素不可能是集合持有者。所以你应该编写你的配置来匹配这个结构(注意我已经为根元素选择了一个名称来匹配你的命名空间,但是你可以随意选择你喜欢的任何东西):

<hachette>
  <EncryptedUserCredentials>
    <EncryptedUserCredential userName="garethB" password="1235@"/>
    <EncryptedUserCredential userName="webService" password="123ffff5@"/>
  </EncryptedUserCredentials>
</hachette>

这意味着您的配置层次结构将有一个根 ConfigSection,而根 ConfigSection 又包含一个 ConfigurationElementCollection,后者包含所有 ConfigurationElement 对象。

这里有一篇关于如何编写它的示例文章:http://www.abhisheksur.com/2011/09/writing-custom-configurationsection-to.html