Configuration system failed to initialize after ad

2019-09-11 04:39发布

问题:

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.

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”

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

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

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.

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:

<?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:

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

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

</configuration>

回答1:

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

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>

Updated GlobalSettings.config:

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

Function code:

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

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"

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.