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.

EF Core 3.1.1 TypeInitializationException (SNI)

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
rbuskovcommented, Feb 4, 2020

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.

1reaction
cheenamalhotracommented, Feb 4, 2020

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to load DLL 'sni.dll' - Entity Framework Core
When using Entity Framework Core in an ASP.Net Core application on Visual Studio 2017 I intermittently get the error "Unable to load 'sni.dll'....
Read more >
SqlClient troubleshooting guide - ADO.NET Provider for ...
TypeInitializationException : The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.
Read more >
System.DllNotFoundException: Unable to load DLL 'sni.dll' or ...
The full error message is: Unhandled exception. Unhandled exception. System.TypeInitializationException: The type initializer for 'System.Data.
Read more >
Could not load file or assembly 'Microsoft.Data.SqlClient
.net-4.7.2.net-standardazure-devopsentity-framework-corenuget. I am calling a .net standard 2.0 library from a framework 4.7.2 test project sucessfully.
Read more >
7.2 Entity Framework Core Support
MySQL Connector/NET integrates support for Entity Framework Core (EF Core). The requirements and ... NET 5.0 (feature set is equivalent to EF Core...
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