添加包含新节的新外部配置文件后,配置系统无法初始化 [英] Configuration system failed to initialize after adding a new external config file containing a new section

查看:273
本文介绍了添加包含新节的新外部配置文件后,配置系统无法初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序通过App.config外部的AppSettings.config和ConnectionStrings.config运行.我要添加另一个配置文件GlobalSettings.config,在其中我将从不想包含在AppSettings.config中的信息中提取信息.

My application runs with the AppSettings.config and ConnectionStrings.config external to the App.config. I want to add another config file GlobalSettings.config in which I will pull in information from that I don't want contained in the AppSettings.config.

在准备问这个问题时,我阅读了以下SO问题,实施了建议的解决方案,但没有找到解决方案:

In preparation to asking this question, I read the following SO questions, implemented suggested solutions, and have not found a solution:

  • Multiple AppSettings files, is it possible?
  • Configuration system failed to initialize error while loading a custom section from app.config
  • configuration system failed to initialize ==> unrecognized configuration section
  • App.Config errors with "Configuration system failed to initialize"

在运行时,我收到一个{配置系统初始化失败"}异常,并带有无法识别的配置节globalSettings"的innerException消息.

On run, I receive a {"Configuration system failed to initialize"} exception with an innerException message of 'Unrecognized configuration section globalSettings.'

以下是我当前的文件.删除App.config的<globalSettings file="GlobalSettings.config"/>行使我的应用程序成功运行.

Below are my current files. Removing the <globalSettings file="GlobalSettings.config"/> line of the App.config allows my application to run successfully.

我已经尝试根据上述链接的建议重新格式化GlobalSettings.config文件.据我了解,我的理解是它应该像在AppSettings.config中声明一个新的configSection一样起作用.根据我的经验,情况似乎并非如此.

I have tried reformatting the GlobalSettings.config file according to the aforementioned links' suggestions. From what I read, it's my understanding that it should work just like declaring a new configSection in the AppSettings.config. From what I am experiencing, that does not seem to be the case.

这时,应用程序中没有代码可以访问GlobalSettings.config,因为它在运行时会中断.

At this time, there is no code in the application to access the GlobalSettings.config as it breaks at runtime.

任何特定的方向和/或参考材料的方向都是可以理解的.请让我知道是否可以提供更多信息.

Any specific direction and/or direction to reference material is appreciated. Please let me know if I may provide more information.

App.config:

App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <appSettings file="AppSettings.config"/>
  <connectionStrings configSource="ConnectionStrings.config"/>
  <globalSettings file="GlobalSettings.config"/>
</configuration>

GlobalSettings:

GlobalSettings:

<?xml version="1.0"?>
<configuration>
  <configSections>
    <section name="globalSettings"
             type="System.Configuration.AppSettingsSection" requirePermission="false" />
  </configSections>

  <globalSettings>
    <add key="key" value="keyValue" />
  </globalSettings>

</configuration>

推荐答案

阅读以下内容后,我开发了一个可行的解决方案:

After reading the following I developed a working solution:

  • Moving a custom configuration group to a separate file
  • An error occurred creating the configuration section handler
  • How to get the values of a ConfigurationSection of type NameValueSectionHandler

更新的App.config:

Updated App.config:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="globalSettings" type="System.Configuration.AppSettingsSection"/>
  </configSections>
  <appSettings file="AppSettings.config"/>
  <connectionStrings configSource="ConnectionStrings.config"/>
  <globalSettings file="GlobalSettings.config"/>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
</configuration>

更新的GlobalSettings.config:

Updated GlobalSettings.config:

<globalSettings>
  <add key="key" value="keyValue" />
</globalSettings>

功能代码:

private void RetrieveKey()
        {
            var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection globalSettingSection = (AppSettingsSection)config.GetSection("globalSettings");
            key = globalSettingSection.Settings["key"].Value;
        }

我发现您需要在App.config中而不是正在创建的配置文件中定义新部分.该部分的类型也很重要.您可以使用以下之一:

I have found that you need to define the new section in the App.config rather than in the config file you're creating. Also the type of the section is important. You can use one of the following:

  • type="System.Configuration.AppSettingsSection"
  • type="System.Configuration.IgnoreSectionHandler
  • type="System.Configuration.NameValueSectionHandler"
  • type="System.Configuration.AppSettingsSection"
  • type="System.Configuration.IgnoreSectionHandler
  • type="System.Configuration.NameValueSectionHandler"

我发现type="System.Configuration.AppSettingsSection"最有用,因为它使配置最像AppSettings.config文件.

I found type="System.Configuration.AppSettingsSection" to be the most useful as it makes the config most like the AppSettings.config file.

请让我知道您是否有任何更改可以使事情变得更有效.

Please let me know if you have any changes that would make things more efficient.

这篇关于添加包含新节的新外部配置文件后,配置系统无法初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆