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

标签 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 类

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

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

最佳答案

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

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

关于c# - 从 app.config 加载自定义部分时配置系统无法初始化错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1327083/

相关文章:

c# - 在 NUnit 3 中使用 [TestCase] 属性测试异常?

c# - 使用 JSON.NET 自定义异常覆盖消息

c# - 毛伊岛的不透明度错误

C#/IronPython Interop 和 "float"数据类型

c# - 如何使用Windows 10操作系统通过IP地址运行visual studio 2017 API项目,我面临很多问题

java - 在浏览器中查看正在运行的应用程序输出,如何?

c# - 在 SharpShell 上下文菜单中选择要重命名的文件

c# - 在 WCF 中通过基类返回派生类时的互操作性

c# - Delegate.EndInvoke 究竟做了什么?有必要打电话吗?

c# - FFMpegConverter 实例不是从 NReco.VideoConverter.dll 版本 1.1.2 创建的