配置系统初始化失败

发布于 2024-11-15 19:30:08 字数 3323 浏览 3 评论 0原文

在我的应用程序中,当我尝试使用此代码获取连接字符串时:

 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>

In my application when I try to get the connection string by using this code :

 System.Configuration.ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;

It throws an exception that "Configuration System Failed To Initialize". Where is the format of app.config wrong ?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-11-22 19:30:08

尝试将 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>

Try putting the configSections as the first child element of configuration, because configSections should be the first element of configurations

So your config file will go like this:

<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>
沉鱼一梦 2024-11-22 19:30:08

我知道这有点晚了,但是当我删除它时:

<startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>

然后错误就消失了。

This is a bit late I know but when I remove this:

<startup>
   <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
</startup>

then the error disappears.

謌踐踏愛綪 2024-11-22 19:30:08

这对我有用。
确保您的 app.config 或 web.config 选项卡内的 标记开头,例如

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

之后确保项目的目标框架属性不是客户配置文件

This works for me.
Make sure that your app.config or web.config starts with the <configSections> tag inside the <configuration> tab e.g

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

after that make sure that the target Framework property of your project is not client profile

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文