Repro steps:
Build https://github.com/aspnet/EntityFramework on windows 7 machine. All tests which uses SqlServer fails. (Tests uses localdb). Same passes in windows10/Server 2012 machines.
Exception message:
Error Message:
System.DllNotFoundException : Unable to load DLL 'sni.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
Stack Trace:
at System.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
at System.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token)
at System.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent()
at System.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory)
at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
dotnet info
.NET Command Line Tools (2.0.0-preview1-005783)
Product Information:
Version: 2.0.0-preview1-005783
Commit SHA-1 hash: 8bd15c7ec0
Runtime Environment:
OS Name: Windows
OS Version: 6.1.7601
OS Platform: Windows
RID: win7-x64
Base Path: C:\Users\smpatel\AppData\Local\Microsoft\dotnet\sdk\2.0.0-preview1-005783\
Microsoft .NET Core Shared Framework Host
Version : 2.0.0-preview1-001967-00
Build : fa25acec41a87ce36b3ea61273d4798eb0a2486f
cc: @Eilon
sni.dll should be getting pulled down from nuget. Is that DLL present in your nuget/packages directiory?
Which package to look for?
More detailed repro details:
Program.cs
```C#
using System;
using System.Data.SqlClient;
namespace repor
{
class Program
{
static void Main(string[] args)
{
var connectionString =
@"Data Source=(localdb)\MSSQLLocalDB;Database=master;Connect Timeout=30";
using (var connection = new SqlConnection(connectionString))
{
connection.Open();
Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
Console.WriteLine("State: {0}", connection.State);
}
Console.WriteLine("Hello World!");
}
}
}
```XML
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Data.SqlClient" Version="4.3.0" />
</ItemGroup>
</Project>
Output
Unhandled Exception: System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.TdsParser' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SNILoadHandle' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'sni.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)
at System.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize(IntPtr pmo)
at System.Data.SqlClient.SNILoadHandle..ctor()
at System.Data.SqlClient.SNILoadHandle..cctor()
--- End of inner exception stack trace ---
at System.Data.SqlClient.TdsParser..cctor()
--- End of inner exception stack trace ---
at System.Data.SqlClient.TdsParser..ctor(Boolean MARS, Boolean fAsynchronous)
at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, BooleanredirectedUserInstance, SqlConnectionString connectionOptions, TimeoutTimer timeout)
at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, Boolean redirectedUserInstance)
at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at System.Data.SqlClient.SqlConnection.Open()
at repor.Program.Main(String[] args)
This is blocking ASP.NET builds.
sni.dll should be coming from https://www.nuget.org/packages/runtime.win7-x64.runtime.native.System.Data.SqlClient.sni. Can you check the assets file to make sure that it is correctly getting picked up from the tooling?
CC @davidfowl
To update this issue. It was discovered the problem is that sni.dll depends on api-sets on Win7 and those are no longer included in .NET Core 2.0 shared framework. So to make this scenario work you either need to directly reference https://www.nuget.org/packages/runtime.win7-x64.Microsoft.NETCore.Windows.ApiSets/ to get the api-set shims or reference the newer version of SqlClient package (>4.4.0).
Using dapper 1.50.2, which has a dependency on SqlClient 4.1, caused this issue. Explicitly installing SqlClient 4.4 indeed solved the issue.
@twDuke This applies to ServiceStack.OrmLite.SqlServer aswell, explicitly installing 4.4 fixes things!
Same issue with NPoco v3.8.0 - manually adding System.Data.SqlClient as a nuget package (v4.4.0) fixed it for me as well.
Minor note: I only had the error during deploy to Azure and not when debugging on localhost.
I have an ASP .NET Core MVC project that is getting this error. Are you saying I need to simply install the
the package: System.Data.SqlClient version 4.4.0 or ???
PM> Install-Package System.Data.SqlClient -version 4.4.0
Is this what I need to do to avoid the above described exception?
I had the same sni.dll error. Installing System.Data.SqlClient (I installed 4.4.2) worked to fix the problem.
@geschw66 Did installing the package fix the issue for you?
.net core 2.1, System.Data.SqlClient 4.6.1 meet the same issue. After install Microsoft.AspNetCore.All 2.1.11 fixed the issue.
@besley Good to know. BTW, please be aware that from dotnet core 2.2 and higher, the Microsoft.AspNetCore.All package doesn't include System.Data.SqlClient anymore. This means from v2.2+ and higher. Should you upgrade in the future to that version, you still need to manually reference System.Data.SqlClient in that case.
Also the Microsoft.AspNetCore.All is quite a big package. If you don't have a direct need for it, it's best to not reference all the stuff in there.
Most helpful comment
To update this issue. It was discovered the problem is that sni.dll depends on api-sets on Win7 and those are no longer included in .NET Core 2.0 shared framework. So to make this scenario work you either need to directly reference https://www.nuget.org/packages/runtime.win7-x64.Microsoft.NETCore.Windows.ApiSets/ to get the api-set shims or reference the newer version of SqlClient package (>4.4.0).