【WinForm】“System.Data.SqlClient.SqlConnection”的類型初始值設定項引發異常,無法識別的配置節 system.serviceModel


出現問題的原因

在本機上沒有出現問題,讓一個同事測試的時候,在另外一台電腦上出現連接數據庫失敗,系統不能打開的問題


在網上搜了一下,有說是數據庫連接字符串錯誤的,有說app.config文件配置不匹配等原因的。總之是沒有解決這個問題

出現問題的錯誤信息,不全,只給出了主要的信息:如下

錯誤描述:Void .ctor()
System.TypeInitializationException: “System.Data.SqlClient.SqlConnection”的類型初始值設定項引發異常。 ---> System.TypeInitializationException: “System.Data.SqlClient.SqlConnectionFactory”的類型初始值設定項引發異常。 ---> System.TypeInitializationException: “System.Data.SqlClient.SqlPerformanceCounters”的類型初始值設定項引發異常。 ---> System.Configuration.ConfigurationErrorsException: 配置系統未能初始化 ---> System.Configuration.ConfigurationErrorsException: 無法識別的配置節 system.serviceModel。 (C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\machine.config line 146)
   在 System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   在 System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   在 System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   在 System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- 內部異常堆棧跟蹤的結尾 ---
   在 System.Configuration.ConfigurationManager.PrepareConfigSystem()
   在 System.Configuration.ConfigurationManager.GetSection(String sectionName)
   在 System.Configuration.ConfigurationManager.get_AppSettings()
   在 log4net.Util.SystemInfo.GetAppSetting(String key)
   --- 內部異常堆棧跟蹤的結尾 ---
   在 System.Data.SqlClient.SqlConnectionFactory..cctor()
   --- 內部異常堆棧跟蹤的結尾 ---
   在 System.Data.SqlClient.SqlConnection..cctor()
   --- 內部異常堆棧跟蹤的結尾 ---
   在 System.Data.SqlClient.SqlConnection..ctor()
   在 System.Data.SqlClient.SqlConnection..ctor(String connectionString)

在machine.config的文件里面的serviceModel信息如下:

<system.serviceModel>
    <extensions>
      <behaviorExtensions>
        <add name="persistenceProvider" type="System.ServiceModel.Configuration.PersistenceProviderElement, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="workflowRuntime" type="System.ServiceModel.Configuration.WorkflowRuntimeElement, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="enableWebScript" type="System.ServiceModel.Configuration.WebScriptEnablingElement, System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="webHttp" type="System.ServiceModel.Configuration.WebHttpElement, System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </behaviorExtensions>
      <bindingElementExtensions>
        <add name="webMessageEncoding" type="System.ServiceModel.Configuration.WebMessageEncodingElement, System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="context" type="System.ServiceModel.Configuration.ContextBindingElementExtensionElement, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </bindingElementExtensions>
      <bindingExtensions>
        <add name="wsHttpContextBinding" type="System.ServiceModel.Configuration.WSHttpContextBindingCollectionElement, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="netTcpContextBinding" type="System.ServiceModel.Configuration.NetTcpContextBindingCollectionElement, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="webHttpBinding" type="System.ServiceModel.Configuration.WebHttpBindingCollectionElement, System.ServiceModel.Web, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add name="basicHttpContextBinding" type="System.ServiceModel.Configuration.BasicHttpContextBindingCollectionElement, System.WorkflowServices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </bindingExtensions>
    </extensions>
    <client>
      <metadata>
        <policyImporters>
          <extension type="System.ServiceModel.Channels.ContextBindingElementImporter, system.workflowservices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"/>
        </policyImporters>
        <wsdlImporters>
          <extension type="System.ServiceModel.Channels.ContextBindingElementImporter, system.workflowservices, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL"/>
        </wsdlImporters>
      </metadata>
    </client>
  </system.serviceModel>

不知道這個信息的作用是什么。

解決方法

問題應該處在了 .net 的C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config 文件里。可能是這期間安裝其他軟件導致配置文件被修改。
解決辦法:將machine.config用同在一個目錄下的machine.config.default替換。

 

 

另:

還有人給出這樣的解決方法,沒有試過,各位遇到這種問題可以自己試一下是否可行


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM