配置系统初始化失败
Posted
技术标签:
【中文标题】配置系统初始化失败【英文标题】:Configuration System Failed To Initialize 【发布时间】:2011-09-16 01:39:03 【问题描述】:在我的应用程序中,当我尝试使用此代码获取连接字符串时:
System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
它会抛出“”的异常。 app.config的格式哪里不对?
<?xml version="1.0"?>
<configuration>
<connectionStrings>
<add name="ConStr" connectionString="Integrated Security=false;Persist Security Info=False;User ID=funny;password=veryfunny;Initial Catalog=vegimanager;Data Source=.\sqlexpress;"/>
</connectionStrings>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Vegi_Manager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<userSettings>
<Vegi_Manager.Properties.Settings>
<setting name="FIRMNAME" serializeAs="String">
<value/>
</setting>
<setting name="FIRMADDRESS" serializeAs="String">
<value/>
</setting>
<setting name="FIRMCITY" serializeAs="String">
<value/>
</setting>
<setting name="FIRMSTATE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMPHONE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMMOBILE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMEMAIL" serializeAs="String">
<value/>
</setting>
<setting name="FIRMTIN" serializeAs="String">
<value/>
</setting>
<setting name="FIRMPAN" serializeAs="String">
<value/>
</setting>
<setting name="FIRMMANDITAXNO" serializeAs="String">
<value/>
</setting>
<setting name="INITIALFONFIGDONE" serializeAs="String">
<value>False</value>
</setting>
<setting name="FIRMJURISDICTION" serializeAs="String">
<value/>
</setting>
<setting name="FIRMBANKDETAILS" serializeAs="String">
<value/>
</setting>
<setting name="FIRMDETAILS" serializeAs="String">
<value/>
</setting>
<setting name="BILLFORMATNO" serializeAs="String">
<value>0</value>
</setting>
<setting name="PRINTERNAME" serializeAs="String">
<value/>
</setting>
</Vegi_Manager.Properties.Settings>
</userSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
【问题讨论】:
您的示例缺少打开的“尝试将configSections
作为configuration
的第一个子元素,因为configSections
应该是配置的第一个元素
所以你的配置文件会是这样的:
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="Vegi_Manager.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false"/>
</sectionGroup>
</configSections>
<connectionStrings>
<add name="ConStr" connectionString="Integrated Security=false;Persist Security Info=False;User ID=funny;password=veryfunny;Initial Catalog=vegimanager;Data Source=.\sqlexpress;"/>
</connectionStrings>
<userSettings>
<Vegi_Manager.Properties.Settings>
<setting name="FIRMNAME" serializeAs="String">
<value/>
</setting>
<setting name="FIRMADDRESS" serializeAs="String">
<value/>
</setting>
<setting name="FIRMCITY" serializeAs="String">
<value/>
</setting>
<setting name="FIRMSTATE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMPHONE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMMOBILE" serializeAs="String">
<value/>
</setting>
<setting name="FIRMEMAIL" serializeAs="String">
<value/>
</setting>
<setting name="FIRMTIN" serializeAs="String">
<value/>
</setting>
<setting name="FIRMPAN" serializeAs="String">
<value/>
</setting>
<setting name="FIRMMANDITAXNO" serializeAs="String">
<value/>
</setting>
<setting name="INITIALFONFIGDONE" serializeAs="String">
<value>False</value>
</setting>
<setting name="FIRMJURISDICTION" serializeAs="String">
<value/>
</setting>
<setting name="FIRMBANKDETAILS" serializeAs="String">
<value/>
</setting>
<setting name="FIRMDETAILS" serializeAs="String">
<value/>
</setting>
<setting name="BILLFORMATNO" serializeAs="String">
<value>0</value>
</setting>
<setting name="PRINTERNAME" serializeAs="String">
<value/>
</setting>
</Vegi_Manager.Properties.Settings>
</userSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
【讨论】:
就像一把热刀穿过黄油!【参考方案2】:我知道这有点晚了,但是当我删除它时:
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>
然后错误消失。
【讨论】:
您必须将 configSections 作为第一个子元素“如果 configSections 元素在配置文件中,则 configSections 元素必须是配置元素的第一个子元素。”。如果您将此配置元素向下移动,它将起作用。【参考方案3】:这对我有用。
确保您的 app.config 或 web.config 以<configuration>
标签内的<configSections>
标记开头,例如
<configSections>
<section name="dataConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=4.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
</configSections>
<connectionStrings>
<add name="ConStr" connectionString="Integrated Security=false;Persist Security Info=False;User ID=funny;password=veryfunny;Initial Catalog=vegimanager;Data Source=.\sqlexpress;"/>
</connectionStrings>
<connectionStrings>
然后确保项目的目标框架属性不是客户端配置文件
【讨论】:
以上是关于配置系统初始化失败的主要内容,如果未能解决你的问题,请参考以下文章
System.Configuration.ConfigurationErrorsException:“配置系统初始化失败”