question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted can't read sni.dll on IIS with custom apppool Identity

See original GitHub issue

We’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:closed
  • Created 4 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
chromigocommented, Mar 16, 2020

Well, I have resolved problem. TLDR: had Microsoft Visual C++ 2015 Redistributable x86 but had not x64.

Details: The exception contains

System.ComponentModel.Win32Exception: The specified module could not be found specified module is keyword here. sni.dll is ok, but probably he still has dependencies.

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

<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Data.SqlClient" publicKeyToken="23ec7fc2d6eaa4a5" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.11.20045.2" newVersion="1.11.20045.2" />
      </dependentAssembly>
    </assemblyBinding>
2reactions
cheenamalhotracommented, Mar 16, 2020

@chromigo

That’s great to know!

I have 1.1.1 version of Microsoft.Data.SqlClient but it look like he still need visual c++ 2015.

You should have reference to Microsoft.Data.SqlClient.SNI v1.1.1 that does not depend on VC++ Redist packages any more.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found