.NET 3.5 - 配置系统未能初始化异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/443235/
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
.NET 3.5 - Configuration system failed to initialize exception
提问by AdamSane
In my winform app, I am trying to add a userSetting, although the error is occuring with appSettings too. When the setting is added I get an exeption thrown that says: "Configuration system failed to initialize" with a Inner Exception "Unrecognized configuration section userSetting"
在我的 winform 应用程序中,我试图添加一个 userSetting,尽管 appSettings 也发生了错误。添加设置后,我收到一个异常提示:“配置系统无法初始化”,内部异常“无法识别的配置部分 userSetting”
Exception Details:
异常详情:
System.Configuration.ConfigurationErrorsException was unhandled
Message="Configuration system failed to initialize"
Source="System.Configuration"
BareMessage="Configuration system failed to initialize"
Line=0
StackTrace:
at System.Configuration.ConfigurationManager.PrepareConfigSystem()
at System.Configuration.ConfigurationManager.RefreshSection(String sectionName)
at System.Configuration.ClientSettingsStore.ReadSettings(String sectionName, Boolean isUserScoped)
at System.Configuration.LocalFileSettingsProvider.GetPropertyValues(SettingsContext context, SettingsPropertyCollection properties)
at System.Configuration.SettingsBase.GetPropertiesFromProvider(SettingsProvider provider)
at System.Configuration.SettingsBase.GetPropertyValueByName(String propertyName)
at System.Configuration.SettingsBase.get_Item(String propertyName)
at System.Configuration.ApplicationSettingsBase.GetPropertyValue(String propertyName)
at System.Configuration.ApplicationSettingsBase.get_Item(String propertyName)
at Settings.get_ApplicationData() in \Properties\Settings.Designer.cs:line 41
at Common.Initialize.IsSettingsInitialized()
at SurveyClient.Program.Main()
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: System.Configuration.ConfigurationErrorsException
Message="Unrecognized configuration section userSettings.
Source="System.Configuration"
BareMessage="Unrecognized configuration section userSettings."
Line=3
StackTrace:
at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
InnerException:
采纳答案by Marc Gravell
Try checking that the app.config (myapp.exe.config once deployed) file exists and has at the top (possibly with other bits)
尝试检查 app.config(部署后的 myapp.exe.config)文件是否存在并且位于顶部(可能还有其他位)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
回答by beluga
I had some user settings, then removed all of them and started seeing this exception - but only when running the app without debugging. It worked fine when debugging the app. The reason is that user-level settings are "cached" in the Local Application Data folder, and in fact are not read from the MYAPP.exe.config. So what I did was go to C:\Users\MYUSERNAME\AppData\Local\MYCOMPANY\MYAPP.exe_Url_longnastyhash9982749827349879\1.0.0.0\user.config (this is on Win7, the path depends on OS) and removed that folder (with the long hash) altogether. The exception went away. BTW, depending on how your settings are setup this user.config file may be under \AppData\Local or \AppData\Roaming.
我有一些用户设置,然后删除了所有这些设置并开始看到这个异常 - 但只有在没有调试的情况下运行应用程序时。调试应用程序时它运行良好。原因是用户级设置被“缓存”在Local Application Data文件夹中,实际上不是从MYAPP.exe.config中读取的。所以我所做的是转到 C:\Users\MYUSERNAME\AppData\Local\MYCOMPANY\MYAPP.exe_Url_longnastyhash9982749827349879\1.0.0.0\user.config(这是在 Win7 上,路径取决于操作系统)并删除该文件夹(使用长哈希)。异常消失了。顺便说一句,根据您的设置如何设置,此 user.config 文件可能位于 \AppData\Local 或 \AppData\Roaming 下。
回答by Kanini
I get this error when I write my App.Config (stupidly) as below.
当我编写我的 App.Config (愚蠢地)如下时,我收到此错误。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<!--List of XMLFiles Begins -->
<add key="ConfigFileEnvironment" value="C:\Program Files\MyProduct\Config\Environment.xml" />
<!--List of XMLFiles Ends -->
</configuration>
Notice, that there is no appSettings? I used to do this mistake regularly...
请注意,没有 appSettings?我以前经常犯这个错误...
回答by Fares
I started seeing this message when I removed all userSettings. I was able to fix it by adding a single userSetting back into the settings files.
当我删除所有 userSettings 时,我开始看到此消息。我能够通过将单个 userSetting 添加回设置文件来修复它。
回答by ashes999
I had some garbage in my machine.config that was causing this error. Look for the exception stack trace and see if you have the same problem. It was essentially malformed XML.
我的 machine.config 中有一些导致此错误的垃圾。查找异常堆栈跟踪,看看您是否有同样的问题。它本质上是格式错误的 XML。
回答by bulltorious
Clean Solution Delete all currently existing settings files as well as app.config Close VS Manually go in and clear out the bin folder and the obj folder of your project Restart PC Re-add the "Application Configuration File"
Clean 解决方案 删除所有当前存在的设置文件以及 app.config 关闭 VS 手动进入并清除您项目的 bin 文件夹和 obj 文件夹重新启动 PC 重新添加“应用程序配置文件”
回答by oddmeter
I had a this problem today and found that I'd accidentally (not to mention erroneously) added a second custom configuration section to my App.config. Once I removed the errant addition, I was able to continue running my application without problems.
我今天遇到了这个问题,发现我不小心(更不用说错误地)在我的 App.config 中添加了第二个自定义配置部分。一旦我删除了错误的添加,我就能够继续运行我的应用程序而不会出现问题。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="ABCConfig" type="ABC.Configuration.ABCConfigurationSection, ABC"/>
<!-- Other custom section definitions -->
</configSections>
<connectionStrings>
<!-- Connection strings go here -->
</connectionStrings>
<!-- Configure ABC -->
<ABCConfig CustomA="blah" CustomB="stuff" />
<!-- Other custom sections -->
<!-- Errant addition to Configure ABC which causes the problem - SHOULD NOT BE HERE -->
<ABCConfig CustomA="blah" CustomB="stuff" />
</configuration>
Removing the second ABCConfig section resolved my problem. Hope this helps!
删除第二个 ABCConfig 部分解决了我的问题。希望这可以帮助!
回答by RA.
Probably the problem is that your config file doesn't comply with its schema. For example, this issue can be recreated by duplication the ConnectionStrings section.
问题可能是您的配置文件不符合其架构。例如,可以通过复制 ConnectionStrings 部分来重新创建此问题。
回答by Dom
I had this problem because I changed one application setting scope (from 'application' to user'). As I couldn't find a solution to resolve my problem, I decided to delete the settings file into the solution explorer. After that, I opened the properties, in the tab 'settings', I clicked where it was proposed to create the settings file. And a new settings file has been created with the values I defined into the previous settings file. I rebuilt my project and it worked well.
我遇到了这个问题,因为我更改了一个应用程序设置范围(从“应用程序”到“用户”)。由于找不到解决问题的解决方案,我决定将设置文件删除到解决方案资源管理器中。之后,我打开了属性,在“设置”选项卡中,我单击了建议创建设置文件的位置。并且使用我在之前的设置文件中定义的值创建了一个新的设置文件。我重建了我的项目,它运行良好。
回答by Snokoh
The startup tag did the trick for me on VS Pro 2013 version 12.021005.1 with .Net 4.5.51650 for console application (i.e. app.config file)
启动标签在 VS Pro 2013 版本 12.021005.1 和 .Net 4.5.51650 控制台应用程序(即 app.config 文件)上对我有用
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
...
</connectionStrings>
</configuration>

