Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'

Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'

Take a look at the following two references:

  1. Unable to load native SNI (Server Name Indication) library

  2. Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll'

Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x86.dll'

Reference the MDS package directly in the .exe projects.

Unable to load native SNI (Server Name Indication) library

Issues in .NET Framework applications

Stacktrace observed:

log
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)
log
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 is the native C++ library that SqlClient depends on for various network operations when running on Windows. In .NET Framework applications that are built with the MSBuild Project SDK, native DLLs aren't managed with restore commands. So a ".targets" file is included in the "Microsoft.Data.SqlClient.SNI" NuGet package that defines the necessary "Copy" operations.

The included ".targets" file is auto-referenced when a direct dependency is made to the "Microsoft.Data.SqlClient" library. In scenarios where a transitive (indirect) reference is made, this ".targets" file should be manually referenced to ensure "Copy" operations can execute when necessary.

Recommended Solution: Make sure the ".targets" file is referenced in the application's ".csproj" file to ensure "Copy" operations are executed.

These targets cover Microsoft's well-known and commonly used targets only. If an external tool or application defines custom targets to copy binaries, new targets must be defined by tool maintainers to ensure native SNI DLLs are copied along-side the Microsoft.Data.SqlClient.dll binaries and are available when executing client applications.

packagesMicrosoft.Data.SqlClient.SNI.2.0.0uild et46Microsoft.Data.SqlClient.SNI.targets

packagesMicrosoft.Data.SqlClient.SNI.2.0.0uildTransitive et46Microsoft.Data.SqlClient.SNI.targets

会添加这个东西

<Import Project="..SolutionpackagesMicrosoft.Data.SqlClient.SNI.2.0.0uild
et46Microsoft.Data.SqlClient.SNI.targets" Condition="Exists('..SolutionpackagesMicrosoft.Data.SqlClient.SNI.2.0.0uild
et46Microsoft.Data.SqlClient.SNI.targets')" />
  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
    <PropertyGroup>
      <ErrorText>This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them.  For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.</ErrorText>
    </PropertyGroup>
    <Error Condition="!Exists('..SolutionpackagesMicrosoft.Data.SqlClient.SNI.2.0.0uild
et46Microsoft.Data.SqlClient.SNI.targets')" Text="$([System.String]::Format('$(ErrorText)', '..SolutionpackagesMicrosoft.Data.SqlClient.SNI.2.0.0uild
et46Microsoft.Data.SqlClient.SNI.targets'))" />
  </Target>
原文地址:https://www.cnblogs.com/chucklu/p/14917619.html