Efcore: EF Core connection Failed to load ..\x86\SNI.dll

Created on 31 Mar 2020  路  9Comments  路  Source: dotnet/efcore

I have a desktop winforms application, which uses entity framework core to work with a SQL Server database.
During the debugging cycle the application runs without problem, makes the connections correctly and executes the queries correctly. When publishing the .exe application, and putting it on the end customer's computer, it fails.

Attached small project to reproduce the error. Download
Attached animated git with evidence. Download

Attention: The problem does not occur on the developer machine with Visual Studio. The problem occurs on the end customer's computer.

Steps to reproduce

  1. Compile the attached project
  2. Copy the results _/Release_ to a Windows folder, on a different computer than the development one. To simulate this I use SandBox of windows 10, to have an isolated space.
  3. Run the WinEFCore.exe application.
  4. Click on the "Click here testing" button. It doesn't matter that the connection string is wrong. The problem can be reproduced as is.
  5. Expected result: The query is executed through the EF Core provider, However, in this case, you should get database connection error.
  6. Current result: Failed to load ..\x86\SNI.dll

Stack trace

``C# System.TypeInitializationException: Se produjo una excepci贸n en el inicializador de tipo de 'Microsoft.Data.SqlClient.TdsParser'. ---> System.TypeInitializationException: Se produjo una excepci贸n en el inicializador de tipo de 'Microsoft.Data.SqlClient.SNILoadHandle'. ---> System.TypeInitializationException: Se produjo una excepci贸n en el inicializador de tipo de 'Microsoft.Data.SqlClient.SNINativeMethodWrapper'. ---> System.ComponentModel.Win32Exception: Failed to load C:\Pruebas\Release\x86\SNI.dll en Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:l铆nea 64 --- Fin del seguimiento de la pila de la excepci贸n interna --- en Microsoft.Data.SqlClient.SNINativeMethodWrapper.SNIInitialize() en Microsoft.Data.SqlClient.SNILoadHandle..ctor() en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:l铆nea 31 en Microsoft.Data.SqlClient.SNILoadHandle..cctor() en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:l铆nea 16 --- Fin del seguimiento de la pila de la excepci贸n interna --- en Microsoft.Data.SqlClient.TdsParser..cctor() en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParser.cs:l铆nea 156 --- Fin del seguimiento de la pila de la excepci贸n interna --- en Microsoft.Data.SqlClient.TdsParser..ctor(Boolean MARS, Boolean fAsynchronous) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParser.cs:l铆nea 32 en Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:l铆nea 1546 en Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:l铆nea 1442 en Microsoft.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, ServerCertificateValidationCallback serverCallback, ClientCertificateRetrievalCallback clientCallback, DbConnectionPool pool, String accessToken, SqlClientOriginalNetworkAddressInfo originalNetworkAddressInfo, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:l铆nea 425 en Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnectionFactory.cs:l铆nea 121 en Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:l铆nea 143 en Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:l铆nea 870 en Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:l铆nea 1807 en Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:l铆nea 1293 en Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) en E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:l铆nea 1191
en Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:l铆nea 302 en Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) en E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:l铆nea 683
en Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:l铆nea 65 en Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) en E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:l铆nea 1605
en Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:l铆nea 1599 en Microsoft.Data.SqlClient.SqlConnection.Open() en E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:l铆nea 1250 en Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) en Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) en Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) en Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.InitializeReader(DbContext _, Boolean result)
en Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.ExecuteTState,TResult
en Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable1.Enumerator.MoveNext() en System.Collections.Generic.List1..ctor(IEnumerable1 collection) en System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
en WinAppEFCore.Form1.button1_Click(Object sender, EventArgs e) en F:\CODFT\WinAppEFCore\WinAppEFCore\Form1.cs:l铆nea 22
en System.Windows.Forms.Control.OnClick(EventArgs e)
en System.Windows.Forms.Button.OnClick(EventArgs e)
en System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
en System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
en System.Windows.Forms.Control.WndProc(Message& m)
en System.Windows.Forms.ButtonBase.WndProc(Message& m)
en System.Windows.Forms.Button.WndProc(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
en System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
en System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

```

Please help. I have stopped the entire publication of a new version of our product.
@ajcvickers @cheenamalhotra @roji @bricelam

Further technical details

EF Core version: 3.1.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Framework 4.6.1
Operating system:
Windows 10 Pro, Visual Studio 2019 16.5.0

closed-question customer-reported

Most helpful comment

Attention: The problem does not occur on the developer machine with Visual Studio. The problem occurs on the end customer's computer.

It maybe due to missing VC++ runtime on customer's machine. Could you try referencing package "Microsoft.Data.SqlClient.SNI:v1.1.1" directly in your project along with Microsoft.Data.SqlClient:1.1.1 as that removes dependency on VC++ Runtime.

All 9 comments

looks related to dotnet/SqlClient#385

Hi, thanks, But my case is with a WinForms desktop application with destination .NET Framework 4.6.1. If you look at the code I attached for testing, it is something very small and normal, and it presents the problem.

@estyfen EF Core 3.1 is a .NET Standard 2.0 library. This means that it has a minimum .NET Framework version of 4.7.2, as described here and here

Are you sure all the machines you are running on have at least 4.7.2 installed?

@estyfen EF Core 3.1 is a .NET Standard 2.0 library. This means that it has a minimum .NET Framework version of 4.7.2, as described here and here

Are you sure all the machines you are running on have at least 4.7.2 installed?

I will immediately check and tell you. Thank you!! @ajcvickers

Hi @ajcvickers ,

I did the review and testing:

  1. I updated the project with target platform: .net framework 4.7.2
  2. I compile and copy the Release in an isolated azure virtual machine with windows 10 Pro 64bits update available with .net framework 4.8, therefore .4.7.2 is already included.
  3. I run the WinAppEFCore.exe
  4. I click on the Test button, and the same error occurs :(

System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception. ---> System.ComponentModel.Win32Exception: Failed to load C:\AppTest\Release\x86\SNI.dll

I think the problem has to do with something that you already worked on here
It has some similarity but I do not see clarity of how it was solved.

@estyfen It's probably related to or a duplicate of one the issues in the SqlClient repo I wanted to make sure you were running on a supported platform before directing you there.

@cheenamalhotra Does this look like a duplicate to you? If not it probably belongs in SqlClient.

@ajcvickers Thanks for your answer. I am going to do the review of open problems in sql repo.

Attention: The problem does not occur on the developer machine with Visual Studio. The problem occurs on the end customer's computer.

It maybe due to missing VC++ runtime on customer's machine. Could you try referencing package "Microsoft.Data.SqlClient.SNI:v1.1.1" directly in your project along with Microsoft.Data.SqlClient:1.1.1 as that removes dependency on VC++ Runtime.

Attention: The problem does not occur on the developer machine with Visual Studio. The problem occurs on the end customer's computer.

It maybe due to missing VC++ runtime on customer's machine. Could you try referencing package "Microsoft.Data.SqlClient.SNI:v1.1.1" directly in your project along with Microsoft.Data.SqlClient:1.1.1 as that removes dependency on VC++ Runtime.

@cheenamalhotra @ajcvickers I could already solve it with your recommendation.
A thousand thanks for giving me back my life. You are a blessing to the community . Until next time!! :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

iberodev picture iberodev  路  3Comments

leak picture leak  路  3Comments

miguelhrocha picture miguelhrocha  路  3Comments

ghost picture ghost  路  3Comments

bgribaudo picture bgribaudo  路  3Comments