【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替换。

 

 

另:

还有人给出这样的解决方法,没有试过,各位遇到这种问题可以自己试一下是否可行

posted @ 2016-02-16 17:37  芝麻学问  阅读(3265)  评论(0编辑  收藏  举报