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

时间:2015-10-19 09:28:40

标签: c# .net-4.0 app-config

我正在尝试在app.config中创建自定义部分并遇到以下异常:

ConfigurationErrorsException

  

无法识别的元素'EncryptedUserCredential'。 (C:\ My Documents \ Hachette.CRM \ test_app_appsettings \ bin \ Debug \ test_app_appsettings.vshost.exe.Config line 11)

现在我完全失去了。我已经进入RegisterEncryptedUserCredentialsConfig.GetConfig(),并发现该部分是无效的 RegisterEncryptedUserCredentialsConfig.EncryptedUserCredentials 。我还在网上调查时检查了所有常见的嫌疑人,例如:

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

    1. 班级图书馆
    2. C#/。NET 4.0。
    3. 我很茫然,并且认为周末过去的时间太长了,需要一些新鲜的眼睛!

      为方便起见,我添加了C#类库here中的所有代码。

      这是app.config:

      <SMS CONTENT="HELLO" UDH="1">
      <ADDRESS TO="9812" FROM="22"/>
      </SMS>
      
      <SMS CONTENT="HELLO" UDH="1">
      <ADDRESS TO="9813" FROM="22"/>
      </SMS>
      
      <SMS CONTENT="HELLO" UDH="1">
      <ADDRESS TO="9814" FROM="22"/>
      </SMS>
      

      以下是EncryptedUserCredential ConfigurationElement

      <?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>
      

      以下是EncryptedCredentials ConfigurationElementCollection

      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;
              }
          }
      }
      

      这是RegisterEncryptedUserCredentialsConfig ConfigurationSection

      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;
          }
      }
      

      以上所有内容都存在于命名空间中:

      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;
              }
      
      
          }
      
      }
      

      在下面的大会上:

      enter image description here

1 个答案:

答案 0 :(得分:2)

根元素不可能是集合持有者。因此,您应该对您的配置进行编码以匹配此结构(请注意,我为根元素选择了一个名称以匹配您的命名空间,但随意选择您喜欢的任何内容):

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

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

以下是有关如何撰写文章的示例文章:http://www.abhisheksur.com/2011/09/writing-custom-configurationsection-to.html

相关问题