Sqlclient: .net framework using entityframeworkcore sni.dll missing

Created on 9 Jan 2020  路  12Comments  路  Source: dotnet/SqlClient

I'm working with a .net framework 4.7.2 application and I am using entityframeworkcore. When I try to access my database I receive the following stacktrace:
```csharp
[Win32Exception (0x80004005): Failed to load C:\repo\bin\x86\SNI.dll]
Microsoft.Data.SqlClient.SNINativeMethodWrapper..cctor() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\Interop\SNINativeMethodWrapper.cs:64

[TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNINativeMethodWrapper' threw an exception.]
Microsoft.Data.SqlClient.SNILoadHandle..ctor() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:31
Microsoft.Data.SqlClient.SNILoadHandle..cctor() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParserSafeHandles.cs:16

[TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.SNILoadHandle' threw an exception.]
Microsoft.Data.SqlClient.TdsParser..cctor() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParser.cs:156

[TypeInitializationException: The type initializer for 'Microsoft.Data.SqlClient.TdsParser' threw an exception.]
Microsoft.Data.SqlClient.TdsParser..ctor(Boolean MARS, Boolean fAsynchronous) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\TdsParser.cs:32
Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:1546
Microsoft.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:1442
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) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlInternalConnectionTds.cs:425
Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnectionFactory.cs:121
Microsoft.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:143
Microsoft.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:870
Microsoft.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:1807
Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:1293
Microsoft.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) in E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionPool.cs:1191 Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionFactory.cs:302
Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionInternal.cs:683 Microsoft.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\ProviderBase\DbConnectionClosed.cs:65
Microsoft.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry) in E:\agent1\_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:1605 Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:1599
Microsoft.Data.SqlClient.SqlConnection.Open() in E:\agent1_work\34\s\src\Microsoft.Data.SqlClient\netfx\src\Microsoft\Data\SqlClient\SqlConnection.cs:1250
Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnection(Boolean errorsExpected) +358
Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open(Boolean errorsExpected) +124
Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReader(RelationalCommandParameterObject parameterObject) +154
Microsoft.EntityFrameworkCore.Query.Internal.Enumerator.InitializeReader(DbContext _, Boolean result) +204
Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.Execute(TState state, Func3 operation, Func3 verifySucceeded) +57
Microsoft.EntityFrameworkCore.Query.Internal.Enumerator.MoveNext() +550
System.Collections.Generic.List1..ctor(IEnumerable1 collection) +186
System.Linq.Enumerable.ToList(IEnumerable`1 source) +54
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +85
System.Web.UI.Control.OnLoad(EventArgs e) +79
System.Web.UI.Control.LoadRecursive() +130
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2852```

Most helpful comment

For now the following Workaround works:

<ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient.sni\1.0.19235.1\buildTransitive\net46\x64\SNI.dll">
      <Link>x64\SNI.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient.sni\1.0.19235.1\buildTransitive\net46\x86\SNI.dll">
      <Link>x86\SNI.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

All 12 comments

Hi @organiccomputer

If this is WPF Application, that's a known issue, see my comment here: https://github.com/dotnet/SqlClient/issues/137#issuecomment-565268512
Otherwise ensure you are using latest driver, refer: https://github.com/dotnet/SqlClient/issues/361#issuecomment-568924833

I am experiencing the same thing, but only on the test server. Locally everything works fine.

In my case there is a .NET Framework 4.7.2 project referencing a .netstandard2.0 project handing Entity Framework and data access. Have not previously referenced any SqlClients directly. I have tried installing Microsoft.Data.SqlClient 1.1.0 on both projects now without seeing any difference.

@cheenamalhotra, my application is actually web forms but the code using ef core is in a separate repository dll. I will also look into the comments you referenced.

@Hoaas

Could you try the repro from link below and confirm if it works in your test environment?
SqlClient Transitive Example

@cheenamalhotra
I have similar issues but your sample works. Instead I have issues if I build a DesktopBridge WPF application. There in the .appxupload file the sni.dll file is missing

You can find my sample project with DesktopBridge sample here: https://github.com/michaelmairegger/SqlClientTransitive

This is the build folder of the Consumer Project
BuildFolder Consumer

This is the build folder of the DesktopBridge Project. Even though that in Consumer build output the x86 and x64 folder with the sni.dll is there, the folder with the content is missing in the DesktopBridge project.
BuildFolder DesktopBridge

For now the following Workaround works:

<ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient.sni\1.0.19235.1\buildTransitive\net46\x64\SNI.dll">
      <Link>x64\SNI.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>
  <ItemGroup>
    <None Include="$(USERPROFILE)\.nuget\packages\microsoft.data.sqlclient.sni\1.0.19235.1\buildTransitive\net46\x86\SNI.dll">
      <Link>x86\SNI.dll</Link>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>
  </ItemGroup>

@cheenamalhotra
Built from my machine it works fine on the server. Haven't set up build and deploy from Azure DevOps. I suspect an older NuGet version might be in use. However many other services and websites built on the same server, all using Entity Framework, without any issues.

@cheenamalhotra, I was able to run the repro successfully. The best that I can come up with is that my class library is using microsoft.entityframeworkcore.sqlserver which uses Microsoft.Data.SqlClient version 1.0.19269.1 rather than 1.1.0.

@cheenamalhotra, I am also converting my class libraries to .net standard 2.0 rather than framework 4.7.2 to see if that helps.

@michaelmairegger

I agree on the issue with WPF .NET framework application transitive ref, I'm looking into it currently. Please continue with the workaround for now in that case or you can also switch to WPF .NET Core application which works fine.

@organiccomputer
It should also work with v1.0.19269.1 since the fix was released before this version in one of the previews. If you followed same steps and still face issues, please upload your repro so we can take a look too.

@cheenamalhotra, I'll begin working on that once I have finished testing a potential fix. Thank you.

@cheenamalhotra, I was able to resolve the issue by converting my class libraries to .net standard rather than framework 4.7.2. I'm going to close the ticket since my issue was resolved. However, I'm still not sure why it would pull in the sni.dll for .net standard and not for .net framework using the same version of microsoft.entityframeworkcore.sqlserver (3.1.0).

Was this page helpful?
0 / 5 - 0 ratings