TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
DllNotFoundException: Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
SNI 是在 Windows 上运行时 SqlClient 完成各种网络操作所依赖的本机 C++ 库。 在使用 MSBuild 项目 SDK 生成的 .NET Framework 应用程序中,本机 DLL 不是通过还原命令管理的。 因此,“Microsoft.Data.SqlClient.SNI”NuGet 包中将包含一个“.targets”文件,用于定义必需的“Copy”操作。
这些目标仅涵盖 Microsoft 的已知和常用目标。 如果外部工具或应用程序定义了用于复制二进制文件的自定义目标,则必须由工具维护人员定义新目标,以确保将本机 SNI Dll 连同 Microsoft.Data.SqlClient.dll 二进制文件一起复制,并在执行客户端应用程序时可用。
.NET Core 应用程序中的问题
观察到 Stacktrace:
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception.
---> System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
---> System.DllNotFoundException: Unable to load shared library 'Microsoft.Data.SqlClient.SNI.dll' or one of its dependencies.
备注
此错误可能仅发生在 Windows 应用程序上。 如果在 Unix 环境中出现此错误,则必须确保构建的应用程序是针对 Unix 运行时,而不是针对 Windows。
SNI 是在 Windows 上运行时 SqlClient 完成各种网络操作所依赖的本机 C++ 库。 Microsoft.Data.SqlClient 不管理此库在 .NET Core 中的加载/卸载。
An assembly specified in the application dependencies manifest (sql2csv.deps.json) was not found:
package: 'Microsoft.Data.SqlClient.SNI.runtime', version: '2.0.0'
path: 'runtimes/win-x64/native/Microsoft.Data.SqlClient.SNI.pdb'
建议的解决方案:确保客户端应用程序引用 Microsoft.Data.SqlClient 包的最小版本 v2.1.0。 使用 EF Core 时,请直接添加对此包版本的 Microsoft.Data.SqlClient 的引用以覆盖依赖项。
主机名解析错误
观察到 Stacktrace:
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 0 - No such host is known.)
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 35 - An internal exception was caught)
Microsoft.Data.SqlClient.SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible.
Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
(provider: TCP Provider, error: 35 - An internal exception was caught)
---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000005, 0xFFFDFFFF): Name does not resolve
可能的原因
未在 SQL Server 上启用 TCP/命名管道协议
建议的解决方案:在 SQL Server 配置管理器控制台的 SQL Server 实例上启用 TCP/命名管道协议。
主机名未知
建议的解决方案:确保主机名从启动连接的客户端解析为服务器的 IP 地址。
登录阶段错误
观察到 Stacktrace:
Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the pre-login handshake.
(provider: SSL Provider, error: 31 - Encryption(ssl/tls) handshake failed)
System.IO.EndOfStreamException: End of stream reached
A connection was successfully established with the server, but then an error occurred during the login process.
(provider: SSL Provider, error: 0 - The target principal name is incorrect.)
Microsoft.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired. The timeout period elapsed during the post-login phase. The connection could have timed out while waiting for server to complete the login process and respond; Or it could have timed out while attempting to create multiple active connections.
The duration spent while attempting to connect to this server was - [Pre-Login] initialization=837; handshake=394; [Login] initialization=3; authentication=15; [Post-Login] complete=1027;
---> System.ComponentModel.Win32Exception (258): Unknown error 258
at Microsoft.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)
当使用 TLS 1.0 或 TLS 1.1 从 Windows/Linux 环境连接到 SqlClient v2.0 以上版本时,如果目标 SQL Server 和客户端在建立连接时无法协商最低的 TLS 版本 1.2,则会引发安全警告消息:Security Warning: The negotiated <TLS1.0 | TLS1.1> is an insecure protocol and is supported for backward compatibility only. The recommended protocol version is TLS 1.2 and later.
Microsoft.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, but then an error occurred during the login process. (provider: SSL Provider, error: 0 - The certificate chain was issued by an authority that is not trusted.)
---> System.ComponentModel.Win32Exception (0x80090325): The certificate chain was issued by an authority that is not trusted.
在客户端环境中安装目标 SQL Server 的 TLS/SSL 证书。 如果需要加密,将对其进行验证。
(不太安全)在连接字符串中设置“TrustServerCertificate=true”属性。
不安全的解决方案:禁用 SQL Server 上的“强制加密”设置。
连接池耗尽错误
观察到 Stacktrace:
System.InvalidOperationException: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool.
This may have occurred because all pooled connections were in use and max pool size was reached.