Only one configSections element allowed per config file and if present must be the first child of the root configuration element

Mahesh picture Mahesh · Nov 29, 2012 · Viewed 51.6k times · Source

I am developing the console application and when I run the .exe file, I get the following error:

system.Configuration.ConfigurationErrorsException: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration> element.

Here's my App.config file:

<configuration>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
    <configSections>
        <section name="Reva.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <!-- ... -->

However, if I remove the following startup section, then it works fine

<startup useLegacyV2RuntimeActivationPolicy="true">
    <supportedRuntime version="v4.0"/>
</startup>

Answer

Daniel Hilgarth picture Daniel Hilgarth · Nov 29, 2012

The error message itself actually details the correct fix:

configSections must be the first child* of the root element:

*emphasis added

So just move the configSections to the top:

<configuration>
    <configSections>
        <section name="Reva.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
    </configSections>
    <startup useLegacyV2RuntimeActivationPolicy="true">
        <supportedRuntime version="v4.0"/>
    </startup>
</configuration>