繁体   English   中英

'System.Data.SqlClient.SqlConnection'的类型初始值设定项引发异常.Windows Server 2016

[英]The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.Windows server 2016

在新的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:方法System.Threading.Interlocked.CompareExchange:类型参数'System.Data.Res'违反了类型参数'TException'的约束。

最后的错误是: type argument 'System.Data.Res' violates the constraint of type parameter 'TException' ,这意味着:它试图用参数调用函数,但该函数不支持它,另一方面,它意味着:重载或更新丢失,因此版本在基本框架中不匹配。

您可以选择解决:

  1. 调试源代码: https : //docs.microsoft.com/zh-cn/visualstudio/debugger/how-to-debug-dotnet-framework-source?view=vs-2017
  2. 启用Funsion Log以获取程序集信息以检查不匹配: https : Funsion Log
  3. 运行dotnet命令以检查您的项目引用了哪些版本,如果未严格配置,则某些程序集将使用更高的版本。

该问题与以下2个帖子类似:

  1. https://github.com/schotime/NPoco/issues/425
  2. https://github.com/dotnet/corefx/issues/18406

版本不匹配时会发生很多情况。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM