Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted can't read sni.dll on IIS with custom apppool Identity
See original GitHub issueWe’re seeing sni.dll error with old asp.net mvc 5 project(net4.8) with Microsoft.EntityFrameworkCore* 3.1.2 installed(and Microsoft.Data.SqlClient.SNI v1.1.1 too). The problem only on full IIS with custom account Identity in app pool configured. Locally on IISExpress all works fine.
Workound for us is to copy C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App\2.*.…sni.dll to application/bin/x64 and replace. (found here https://github.com/dotnet/SqlClient/issues/385)
Is there some more convinient solution of this problem?
Exception message:
System.TypeInitializationException
The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception.
Stack trace:
System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load C:\www\site3\site\bin\x64\SNI.dll ---> System.ComponentModel.Win32Exception: The specified module could not be found
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor()
--- End of inner exception stack trace ---
at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted)
at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token)
at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent()
at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory)
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup)
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.InitializeReader(DbContext _, Boolean result)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1.Enumerator.MoveNext()
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Unable to load DLL 'sni.dll' - Entity Framework Core
Adding nuget package System.Data.SqlClient v4.4.0 solved the same problem for me. The problem is not specific to Entity Framework.
Read more >Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'
I have two published environment, Staging and one for live. my published project works in test env. without any issue, but as soon...
Read more >Unable to load DLL 'Microsoft.Data.SqlClient.SNI.x64.dll'
Hi , I am having an legacy vb.net application which is using .net 4.7.2. I replaced System.Data.Sqlclient with Microsoft.Data.
Read more >Can't change identity of application pool
Error when you change the identity of an application pool by using IIS Manager from a remote computer: Keyset does not exist.
Read more >Bug in SSMS v19 Preview 4.0 - Microsoft Q&A
Cannot connect to ANY database on my development machine with version 19.0 Preview 4.0. Will need to roll back in order just to...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Well, I have resolved problem. TLDR: had Microsoft Visual C++ 2015 Redistributable x86 but had not x64.
Details: The exception contains
So I looked at https://github.com/dotnet/SqlClient/issues/211 and https://github.com/dotnet/SqlClient/issues/361 I already checked that machine has Microsoft Visual C++ 2015 Redistributable installed. But it was only x86 version… Installing x64 solved the problem.
It’s strange, because here https://github.com/dotnet/SqlClient/issues/361#issuecomment-594759511 say that “we have removed dependency on VC Runtime, so you don’t need that installed on your client”. I have 1.1.1 version of Microsoft.Data.SqlClient but it look like he still need visual c++ 2015. binding redirect
@chromigo
That’s great to know!
You should have reference to
Microsoft.Data.SqlClient.SNIv1.1.1 that does not depend on VC++ Redist packages any more.