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

C#asp.netIis 7Console ApplicationApp Config

C# Problem Overview


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>

C# Solutions


Solution 1 - C#

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>

Solution 2 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMaheshView Question on Stackoverflow
Solution 1 - C#Daniel HilgarthView Answer on Stackoverflow
Solution 2 - C#MAFAIZView Answer on Stackoverflow