每个配置文件只允许一个 configSections 元素,并且 if 必须是根配置元素的第一个子元素

我正在开发这个控制台应用,当我运行.exe 文件时,我得到以下错误:

system.Configuration.ConfigurationErrorsException: 每个配置文件只允许一个 <configSections>元素,如果存在,则必须是根 <configuration>元素的第一个子元素。

这是我的 App.config文件:

<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>
<!-- ... -->

但是,如果我删除以下 startup部分,那么它的工作正常吗

<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
59487 次浏览

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>

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