Sqlclient: EF Core 3.1.1 TypeInitializationException (SNI)

Created on 3 Feb 2020  路  9Comments  路  Source: dotnet/SqlClient

I get the following exception whenever I try to connect to SQL Server with EF Core:

System.TypeInitializationException:
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.Win32NativeMethods.IsTokenRestrictedWrapper (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPool (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpenInner (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.SqlClient.SqlConnection.TryOpen (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at Microsoft.Data.SqlClient.SqlConnection.OpenAsync (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection+<OpenDbConnectionAsync>d__50.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection+<OpenDbConnectionAsync>d__50.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.Storage.RelationalConnection+<OpenAsync>d__47.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.Storage.RelationalCommand+<ExecuteReaderAsync>d__17.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1+AsyncEnumerator+<InitializeReaderAsync>d__18.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy+<ExecuteAsync>d__7`2.MoveNext (Microsoft.EntityFrameworkCore.SqlServer, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable`1+AsyncEnumerator+<MoveNextAsync>d__17.MoveNext (Microsoft.EntityFrameworkCore.Relational, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions+<ToListAsync>d__64`1.MoveNext (Microsoft.EntityFrameworkCore, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions+<ToListAsync>d__64`1.MoveNext (Microsoft.EntityFrameworkCore, Version=3.1.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore.BlueGreenTable+<GetGreenEntities>d__5`1.MoveNext (SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullSCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\Pools\Vsts\_work\1290\s\source\SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore\BlueGreenTable.cs)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore.InvoiceReadStore+<GetReadModel>d__12.MoveNext (SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullSCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\Pools\Vsts\_work\1290\s\source\SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore\Invoice\InvoiceReadStore.csSCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 94)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore.InvoiceReadStore+<Get>d__6.MoveNext (SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullSCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\Pools\Vsts\_work\1290\s\source\SCBDK.CoreBanking.ServiceDomains.Payment.ReadStore\Invoice\InvoiceReadStore.csSCBDK.CoreBanking.ServiceDomains.Payment.ReadStore, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 40)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at SCBDK.CoreBanking.ServiceDomains.Payment.CoreView.Adapters.GetInvoiceAdapter+<GetFromReadStore>d__6.MoveNext (SCBDK.CoreBanking.ServiceDomains.Payment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullSCBDK.CoreBanking.ServiceDomains.Payment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\Pools\Vsts\_work\1290\s\source\SCBDK.CoreBanking.ServiceDomains.Payment\CoreView\Adapters\GetInvoiceAdapter.csSCBDK.CoreBanking.ServiceDomains.Payment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 55)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at SCBDK.CoreBanking.ServiceDomains.Payment.CoreView.Adapters.GetInvoiceAdapter+<GetInvoice>d__4.MoveNext (SCBDK.CoreBanking.ServiceDomains.Payment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=nullSCBDK.CoreBanking.ServiceDomains.Payment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: D:\Pools\Vsts\_work\1290\s\source\SCBDK.CoreBanking.ServiceDomains.Payment\CoreView\Adapters\GetInvoiceAdapter.csSCBDK.CoreBanking.ServiceDomains.Payment, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null: 40)
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089)
   at SCBDK.CoreBanking.ServiceDomains.AdapterContext`2+<ExecuteAdapter>d__17.MoveNext (SCBDK.CoreBanking.ServiceDomains, Version=3.0.11.0, Culture=neutral, PublicKeyToken=null)
Inner exception System.ComponentModel.Win32Exception handled at Microsoft.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted:
   at Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor (Microsoft.Data.SqlClient, Version=1.0.19269.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5)

The code works fine on my dev box but not when I deploy.

EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: net 4.7.2
Operating system: Windows Server 2016 Standard
.Net Framework version: 4.8
Nuget version: 5.3.0
IDE: Visual Studio 2019

I have read about several similar issues/fixes but nothing seems to work for me. I have tried adding the following packages to the project:

System.Data.SqlClient 4.8
Microsoft.Data.Client 1.0

Also, I have updated Net Framework and VC++ Redistributable on the server to the latest versions.

Native SNI

Most helpful comment

My issue is not the same as as #354.

In my case, SNI.dll is not locked an the error message is:

The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. Failed to load C:\Octopus\...\1.0.6\x86\SNI.dll The specified module could not be found

Although SNI.dll is in fact present (and unlocked) in the folder in question.

All 9 comments

@rbuskov The issue seems to be coming from the SqlClient ADO.NET provider. It could be the same root cause as https://github.com/dotnet/SqlClient/issues/354.

Moving to that repo.

Looks like I don't have permissions to move the issue.

@cheenamalhotra It would be good if we could use the GitHub issue mover to easily move issues between EF and SqlClient. If you agree, then I'll look into the permissions needed.

Sure, I have granted you permissions, please try again.

@cheenamalhotra Still don't seem to be able to transfer.

@anurse I think you might have looked into this at some point. Do you know what permissions we need here to enable bi-directional transfer of issues?

I don't have permission either. The docs say:

To transfer an open issue to another repository, you must have write permissions on the repository the issue is in and the repository you're transferring the issue to. For more information, see "Repository permission levels for an organization."

Oops! I should have given Write permissions then.
@ajcvickers would you mind trying again?

Success!

My issue is not the same as as #354.

In my case, SNI.dll is not locked an the error message is:

The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. Failed to load C:\Octopus\...\1.0.6\x86\SNI.dll The specified module could not be found

Although SNI.dll is in fact present (and unlocked) in the folder in question.

Hi @rbuskov

I took a deeper look and seems like this method "UnmanagedIsTokenRestricted" makes a Win32 API call to IsTokenRestricted(token) which has conditional execution in Win SDK 10:

#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)

WINADVAPI
BOOL
WINAPI
IsTokenRestricted(
    _In_ HANDLE TokenHandle
    );


#endif /* WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM) */

It doesn't look like this was a case in Win 8.1 SDK, so I'm wondering these conditions are causing a problem here.

Was this page helpful?
0 / 5 - 0 ratings