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

3

103

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>
Absentminded answered 29/11, 2012 at 10:39 Comment(1)
In German, the error message reads "Pro Konfigurationsdatei ist nur ein <configSections>-Element zulässig und muss, sofern vorhanden, das erste untergeordnete Element des Stamm-<configuration>-Elements sein." (Just in case, someone is googling this one).Saratov
L
251

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>
Lohman answered 29/11, 2012 at 10:41 Comment(6)
Today I learnt I should read the whole error message. Thanks.Mathew
@AtronSeige me too :PZacatecas
I got the same error message for a windows service and the app.config doesn't even have a "<configSections>" tag in it.Sperry
Please post your config to pastebin and Link it hereLohman
As it turned out, my config was OK but the machine.config on that machine was broken. It even contained an SQLExpress/localhost connection string although no SQL was ever installed on that machine.Sperry
If you rearranged configSections as first element and you still have same error then delete everything in "Solution/bin" folder and try again.Monocot
T
2

The Error web.config File

 <?xml version="1.0" encoding="utf-8"?>   

<configuration>    
   <connectionStrings>   
      <add name="SQLConnect" 
           connectionString="Data Source=SAHIL; Initial Catalog=Demo; Integrated Security=SSPI" 
           providerName="System.Data.SqlClient" />   
   </connectionStrings>     

   <configSections>   
      <sectionnamesectionname="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, 
          Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> 
   </configSections>  

   :   
   :   
   :   
   :   
   :   
   :   
   :   
</configuration> 

The Error Was

enter image description here

To fix the error, I rearranged the elements and the error was fixed.

enter image description here

Type answered 30/5, 2017 at 3:29 Comment(0)
O
1

For my case:

Below app.config was giving run time error as

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

Faulty App.config:

<configuration> 
    <appSettings></appSettings>
    <configSections></configSections>   
    <startup> </startup>
    <runtime></runtime>
</configuration>

I just moved <configSections></configSections> on the top and problem resolved.

Correct App.config:

<configuration> 
    <configSections></configSections>
    <appSettings></appSettings>
    <startup> </startup>
    <runtime></runtime>
</configuration>
Osbourn answered 9/2, 2023 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.