C# '的类型初始值设定项;System.Data.SqlClient.SqlConnection';引发了异常。Windows server 2016

C# '的类型初始值设定项;System.Data.SqlClient.SqlConnection';引发了异常。Windows server 2016,c#,windows,sqlconnection,C#,Windows,Sqlconnection,在新的windows server 2016上,当wcf服务尝试使用实体框架连接数据库(sql server 2017)时,我们遇到异常。相同的代码在许多机器上工作 The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. errordetails~ExceptionMessage - The type initializer for 'System.Data.SqlClient.Sql

在新的windows server 2016上,当wcf服务尝试使用实体框架连接数据库(sql server 2017)时,我们遇到异常。相同的代码在许多机器上工作

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.  errordetails~ExceptionMessage - The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. 
ExceptionSource - System.Data  
ExceptionTarget - Void .ctor()  
StackTrace - System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.Common.ADP' threw an exception. ---> System.Security.VerificationException: Method System.Threading.Interlocked.CompareExchange: type argument 'System.Data.Res' violates the constraint of type parameter 'TException'.
   at System.Data.Res.GetLoader()
   at System.Data.Res.GetString(String name)
   at System.Data.Common.ADP..cctor()
   --- End of inner exception stack trace ---
   at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
   at System.Data.SqlClient.SqlPerformanceCounters..ctor()
   at System.Data.SqlClient.SqlPerformanceCounters..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnectionFactory..ctor()
   at System.Data.SqlClient.SqlConnectionFactory..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnection..cctor()
   --- End of inner exception stack trace ---
   at System.Data.SqlClient.SqlConnection..ctor()
   at System.Data.SqlClient.SqlClientFactory.CreateConnection()
   at System.Data.EntityClient.EntityConnection.GetStoreConnection(DbProviderFactory factory)
   at System.Data.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString)
   at System.Data.EntityClient.EntityConnection..ctor(String connectionString)
   at System.Data.Objects.ObjectContext.CreateEntityConnection(String connectionString)
   at System.Data.Objects.ObjectContext..ctor(String connectionString, String defaultContainerName)
   at EntityDataAccessLayer.dbConnString..ctor()
顶部显示内部异常的完整列表:

StackTrace-System.TypeInitializationException:“System.Data.SqlClient.SqlConnection”的类型初始值设定项引发异常。-->System.TypeInitializationException:“System.Data.SqlClient.SqlConnectionFactory”的类型初始值设定项引发异常。-->System.TypeInitializationException:“System.Data.SqlClient.SqlPerformanceCounters”的类型初始值设定项引发异常。-->System.TypeInitializationException:“System.Data.Common.ADP”的类型初始值设定项引发异常。-->System.Security.VerificationException:Method System.Threading.Interlocated.CompareeExchange:type参数“System.Data.Res”违反了类型参数“TexException”的约束


最后一个错误是:
类型参数'System.Data.Res'违反了类型参数'TException'
的约束,这意味着:它试图使用参数调用函数,但函数不支持它,另一方面,这意味着:重载或更新丢失,因此基本框架中的版本不匹配

您可以选择如何解决此问题:

  • 在源代码上调试:
  • 启用
    Funsion Log
    以获取部件信息以检查不匹配:
  • 运行以检查项目引用的版本,如果未严格配置,则某些程序集正在使用更高版本
  • 这两个员额的问题类似:

  • 当版本不匹配时,这种情况经常发生

    您可以尝试升级所有相关软件包,包括框架本身


    如果核心库
    System.Data.SqlClient
    太旧,无法与其他包的最新版本匹配,则必须降级项目中的其他包。

    是否可以发布连接字符串?类型初始化器异常表示静态构造函数抛出。如果这是一个框架类型,我将首先修复计算机上的.NET framework安装。异常消息是什么?ExceptionMessage-System.Data.SqlClient.SqlConnection的类型初始值设定项引发了异常。ExceptionSource-System.Data ExceptionTarget-Void.ctor()版本没有问题,因为它正在另一台具有相同配置的服务器上工作。@没有环境,Simplyvaibh很难找到它。但启用融合日志可以帮助您立即找出真正的问题所在。答案已更新。