configuration system failed to initialize ==> unre

2019-09-10 08:44发布

问题:

I don't even know if I can do what I'm attempting but I've imported forms from several projects and added references to those projects. Each project has a different set of connection strings and I'm trying to get them to coexist in App.config where I can filter by SECTION (Users select connections from comboboxes). I am hoping I can do this by implementing ConfigSections. If it's doable I obviously don't know how.

Attached is my App.config. I'm getting the error 'configuration system failed to initialize' and when I drill into the detail it says 'unrecognized configuration section amSettings

Is what I'm trying to do possible? If so, what do I need to correct?

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="ApplicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="amSettings.Properties.Settings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     allowExeDefinition="MachineToLocalUser" 
                     requirePermission="false"/>
            <section name="cbSettings.Properties.Settings"
         type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         requirePermission="false"/>
        </sectionGroup>
    </configSections>

    <amSettings>
        <add key="VX130 Attribute Map Connections" value="Sample Console Application" />
        <add key="Region 1 VX130"   value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 VX130"   value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 VX130"   value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 VX130"   value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="CDW"  value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
    </amSettings>
    <cbSettings>
        <add key="CDW Class Builder Connections" value="Sample Console Application" />
        <add key="Region 1 Class Build"     value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 Class Build"     value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 Class Build"     value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 Class Build"     value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="DEVELOPMENT Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="PREVIEW Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="VERSION Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
    </cbSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

回答1:

Change the section name from amSettings.Properties.Settings to amSettings and cbSettings.Properties.Settings to cbSettings

e.g.

    `<section name="amSettings" `

Here is a comprehensive example:

If you change your config file to this:

    <configSections>
        <section name="amSettings"
                 type="System.Configuration.AppSettingsSection"
                 allowExeDefinition="MachineToLocalUser"
                 requirePermission="false"/>
        <section name="cbSettings"
     type="System.Configuration.AppSettingsSection"
     requirePermission="false"/>
  </configSections>
  <amSettings>
      <add key="ABC" value="DEF"/>
  </amSettings>

Then you can access the key ABC using this code:

        var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        AppSettingsSection appSettingSection = (AppSettingsSection)config.GetSection("amSettings");
        var a = appSettingSection.Settings["ABC"].Value;


回答2:

The solution was two things. Change section name as user469104 recommended and wrapping sections in Group Name.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="ApplicationSettings" 
                      type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="amSettings" 
                     type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" 
                     allowExeDefinition="MachineToLocalUser" 
                     requirePermission="false"/>
            <section name="cbSettings"
         type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
         requirePermission="false"/>
        </sectionGroup>
    </configSections>
<ApplicationSettings>
    <amSettings>
        <add key="VX130 Attribute Map Connections" value="Sample Console Application" />
        <add key="Region 1 VX130"   value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 VX130"   value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 VX130"   value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 VX130"   value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="CDW"  value="Server=VHACDWA01;Database=;Trusted_Connection=true;"/>
    </amSettings>
    <cbSettings>
        <add key="CDW Class Builder Connections" value="Sample Console Application" />
        <add key="Region 1 Class Build"     value="Server=R01SCRDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 2 Class Build"     value="Server=R02LITDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 3 Class Build"     value="Server=R03DURDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="Region 4 Class Build"     value="Server=R04PHIDWH82;Database=R01_FDW;Trusted_Connection=true;"/>
        <add key="DEVELOPMENT Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="PREVIEW Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
        <add key="VERSION Class Build"  value="Server=VHACDWA01;Database=Util;Trusted_Connection=true;"/>
    </cbSettings>
</ApplicationSettings>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>