【问题标题】:Configuration system failed to initialize error while loading a custom section from app.config从 app.config 加载自定义部分时,配置系统无法初始化错误
【发布时间】:2010-11-22 13:28:51
【问题描述】:

我通过以下方式在我的 app.config 中定义了一个配置部分:

    <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="RegisterCompanies"
             type="ConfigTest.RegisterCompaniesConfig, ConfigTest" 
             allowLocation="true" 
             allowDefinition="Everywhere"/>
          </configSections>      
  <RegisterCompanies>
    <Companies>
      <Company name="Tata Motors" code="Tata"/>
      <Company name="Honda Motors" code="Honda"/>
    </Companies>
  </RegisterCompanies>      
      </configuration>

为了阅读这些信息,我以这种方式创建了三个类:RegisterCompaniesConfig 类

public class RegisterCompaniesConfig : ConfigurationSection
    {
        public static RegisterCompaniesConfig GetConfig() 
        {
            string path = Path.Combine(Application.StartupPath, "ConfigTest.exe.config");
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(path);
            RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;
            return serviceSection;
            //return (RegisterCompaniesConfig)System.Configuration.ConfigurationManager.GetSection("RegisterCompanies");
        }

        [System.Configuration.ConfigurationProperty("Companies")]
        public Companies Companies 
        {
            get
            {
                object o = this["Companies"]; return o as Companies;
            }
        }
    }

然后是公司类:

public class Companies : ConfigurationElementCollection
    {
       [System.Configuration.ConfigurationProperty("Company")]
        public Company this[int index] 
        {
            get
            {
                return base.BaseGet(index) as Company;
            } 
            set 
            { 
                if (base.BaseGet(index) != null)
                { base.BaseRemoveAt(index); } this.BaseAdd(index, value);
            }
        }
        protected override ConfigurationElement CreateNewElement()
        {
            return new Company();
        }

        protected override object GetElementKey(ConfigurationElement element)
        {            
            return ((Company)element).Name;
        }
    }

最后一个是公司类:

public class Company : ConfigurationElement
    {
        [ConfigurationProperty("name", IsRequired = true)]
        public string Name 
        {
            get 
            {
                return this["name"] as string; 
            } 
        }
        [ConfigurationProperty("code", IsRequired = true)]
        public string Code
        {
            get
            {
                return this["code"] as string; 
            } 
        }
    }

之后,当我想通过调用以下方法访问该部分时

var config = RegisterCompaniesConfig.GetConfig();

我收到错误:配置系统初始化失败 请任何人看看上面的代码,问题出在哪里,它看起来对我来说一切都很好......

【问题讨论】:

  • 问题一定出在app.config文件中,可以完整显示吗?
  • 我已经添加了完整的app.config文件,请看一下...
  • 您能否针对您收到的错误显示堆栈跟踪?
  • 另外,如果设置了 InnerException,它可能会有用...
  • 我发现了这个错误,实际上当我用 add 替换公司元素时它工作正常.. 无论如何谢谢指导我

标签: c#


【解决方案1】:

刚刚运行您的代码,我收到的错误是“元素 可能只在本节中出现一次”:

RegisterCompaniesConfig serviceSection = ConfigurationManager.GetSection("RegisterCompanies") as RegisterCompaniesConfig;

这似乎表明您只能使用当前获得的代码在其中包含一个公司元素。

过去我使用以下方法没有任何问题:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="Libraries">
      <section name="MyLibrary" type="System.Configuration.NameValueSectionHandler,system, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, Custom=null" />
    </sectionGroup>
  </configSections>

  <Libraries>
    <MyLibrary>
      <add key="Test" value="Test1"/>
    </MyLibrary>
  </Libraries>
</configuration>

然后我使用如下代码访问它:

public string GetValue(string configurationKey, string defaultValue)
{
  NameValueCollection _config = (NameValueCollection)ConfigurationManager.GetSection("Libraries/MyLibrary");
  string result = (_config == null) ? null : _config[configurationKey];
  return (result == null ? defaultValue : result);
}

如果你没有拥有名为“name”和“code”的属性,那么你可以只使用上面的代码,否则你可以使用 Reflector 来了解 NameValueCollection 的作用并从那里开始工作!

【讨论】:

  • 我发现了这个错误,实际上当我用 add 替换公司元素时它工作正常.. 无论如何谢谢指导我
猜你喜欢
  • 2019-01-20
  • 2014-03-08
  • 2013-09-07
  • 2017-04-29
  • 2018-03-23
  • 2014-09-28
  • 1970-01-01
  • 2013-11-02
  • 2016-05-13
相关资源
最近更新 更多