System.Configuration.ConfigurationErrorsException - 无法识别的元素'设置'

时间:2014-12-03 18:50:04

标签: c# configurationmanager

  

无法识别的元素'设置'。 (C:\ Dev \ DOT.NET \ AutoPay \ ibeam.config第15行)

     

AppInfo.Sections.Get(“AutoPay.Common.Credentials”)'AppInfo.Sections.Get(“AutoPay.Common.Credentials”)'   抛出了类型的例外   'System.Configuration.ConfigurationErrorsException'System.Configuration.ConfigurationSection   {System.Configuration.ConfigurationErrorsException}

我的配置文件

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
<sectionGroup name="AppInfo" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
  <section name="AutoPay.Common.Credentials" 
           type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
               allowExeDefinition="MachineToLocalUser" 
               requirePermission="false" />
    </sectionGroup>
  </configSections>

  <AppInfo>
    <AutoPay.Common.Credentials>
      <setting name="UserName" serializeAs="String"><value>********</value></setting>
      <setting name="Password" serializeAs="String"><value>********</value></setting>
      <setting name="ServiceUrl" serializeAs="String"><value>********</value></setting>
    </AutoPay.Common.Credentials>
  </AppInfo>
</configuration>

基础(抽象)类

public abstract class BaseConfigInfo : ConfigurationSectionGroup
{
    protected Configuration Configuration;
    protected ConfigurationSectionGroup AppInfo;
    protected ClientSettingsSection Credentials;

    protected BaseConfigInfo(string configFile)
    {
        var fileMap = new ConfigurationFileMap(configFile);
        Configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
        AppInfo = Configuration.GetSectionGroup("AppInfo");

        // THIS IS WHERE THE ERROR POPS //
        Credentials = (ClientSettingsSection)AppInfo.Sections.Get("AutoPay.Common.Credentials");
    }

    protected string GetCredentialsString(string kvpName)
    {
        var setting = Credentials.Settings.Get(kvpName);
        return setting.Value.ValueXml.InnerText;
    }

    public string UserName { get { return GetCredentialsString("UserName"); } }
    public string Password { get { return GetCredentialsString("Password"); } }
    public string ServiceUrl { get { return GetCredentialsString("ServiceUrl"); } }
}

示例具体类

public class ConfigInfoIbeam : BaseConfigInfo
{
    public ConfigInfoIbeam() : base(ConfigurationManager.AppSettings["ConfigInfoIbeam"]) { }
}

我知道我在这里缺少一些简单的东西。我在.NET 4中运行。当我尝试谷歌这个错误时,没有什么太有用了。有什么想法吗?

2 个答案:

答案 0 :(得分:3)

我发现了问题。在我的配置中我有这个:

  <AppInfo>
    <AutoPay.Common.Credentials>
      <setting name="UserName" serializeAs="String"><value>********</value></setting>
      <setting name="Password" serializeAs="String"><value>********</value></setting>
      <setting name="ServiceUrl" serializeAs="String"><value>********</value></setting>
    </AutoPay.Common.Credentials>
  </AppInfo>

我把它改为:

  <AppInfo>
    <AutoPay.Common.Credentials>
      <setting name="UserName" serializeAs="String">
        <value>********</value>
      </setting>
      <setting name="Password" serializeAs="String">
        <value>********</value>
      </setting>
      <setting name="ServiceUrl" serializeAs="String">
        <value>********</value>
      </setting>
    </AutoPay.Common.Credentials>
  </AppInfo>

不要问我为什么底层的xml阅读器无法区分这两者。但问题解决了! GRRRR ...

答案 1 :(得分:0)

我认为您需要<settings> </settings>包裹您的三个<setting> .....