从app.config加载自定义部分时,配置系统无法初始化错误

时间:2009-08-25 09:19:24

标签: c#

我已经通过以下方式在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 class

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();

我收到错误:配置系统无法初始化 请大家看看上面的代码,问题出在哪里,它看起来一切都对我好....

1 个答案:

答案 0 :(得分:0)

刚刚运行了您的代码,我收到的错误是“元素&lt; Company&gt;可能只在本节中出现一次”在该行上:

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的作用并从那里开始工作!