C# 每个配置文件只允许一个 configSections 元素,如果存在,必须是根配置元素的第一个子元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13623896/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 09:14:15  来源:igfitidea点击:

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

提问by Mahesh

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

我正在开发控制台应用程序,当我运行 .exe 文件时,出现以下错误:

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

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

Here's my App.configfile:

这是我的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>
    <!-- ... -->

However, if I remove the following startupsection, then it works fine

但是,如果我删除以下startup部分,则它可以正常工作

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

采纳答案by Daniel Hilgarth

The error message itself actually details the correct fix:

错误消息本身实际上详细说明了正确的修复:

configSectionsmust be the first child* of the root element:

configSections必须是根元素的第一个子元素 *:

*emphasis added

*强调添加

So just move the configSectionsto the top:

所以只需将 移动configSections到顶部:

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

回答by MAFAIZ

The Error web.config File

错误 web.config 文件

 <?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

在此处输入图片说明