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

问题描述 投票:0回答:1

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

c# windows sqlconnection
1个回答
0
投票

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

你可以选择搞清楚:

  1. 调试源代码:https://docs.microsoft.com/en-us/visualstudio/debugger/how-to-debug-dotnet-framework-source?view=vs-2017
  2. 启用Funsion Log以获取汇编信息以检查不匹配:https://docs.microsoft.com/en-us/dotnet/framework/tools/fuslogvw-exe-assembly-binding-log-viewer
  3. 运行dotnet command以查看项目引用的版本,如果没有严格配置,某些程序集使用更高版本。

问题与这两个帖子类似:

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

当版本不匹配时,它会发生很多。

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

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

© www.soinside.com 2019 - 2024. All rights reserved.