Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'.

Strangely though I have found a temporary fix: restarting my PC.

I don't know why the error keeps happening, I don't know what the variable could be. Could anyone shed light on the possible cause? I would offer a full stack trace but it hasn't happened since deciding to post on here, when it next happens I'll be sure to update this. I know for certain though (as the debug catches the exception) that it happens when EF has cause to get data from the db and transform to entities.

Thanks in advance for any suggestions.

--EDIT This is still happening. Managed to remember to capture the stack trace:

System.DllNotFoundException occurred HResult=0x80131524 Message=Unable to load DLL 'sni.dll': The specified network name is no longer available. (Exception from HRESULT: 0x80070040) Source= StackTrace: at System.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr token, Boolean& isRestricted) at System.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr token) at System.Data.ProviderBase.DbConnectionPoolIdentity.GetCurrent() at System.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool(DbConnectionFactory connectionFactory) at System.Data.ProviderBase.DbConnectionFactory.GetConnectionPool(DbConnection owningObject, DbConnectionPoolGroup connectionPoolGroup) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.Open() at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer) at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func2 operation, Func2 verifySucceeded, TState state) at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__31.MoveNext() at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__301.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass20_01.<CompileQueryCore>b__0(QueryContext qc) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable1 source)

like image 354
TheBadgerEB Avatar asked Jul 28 '17 21:07

TheBadgerEB


3 Answers

Adding nuget package System.Data.SqlClient v4.4.0 solved the same problem for me. The problem is not specific to Entity Framework. It is caused by older version of SqlClient dependency on APIs not available in dotnet Core 2.

like image 112
regmagik Avatar answered Oct 06 '22 17:10

regmagik


In some instances this issue can be caused by IIS not having sufficient permissions to access areas of your %USERPROFILE%.nuget

This can be solved in one of two simple ways:

Grant permission to the IIS appPool (like DefaultAppPool or FooAppPool) to access not only the .Nuget folder (this is usually done automatically).

Set the AppPool being used to run as 'LocalSystem' under Identity, this way the appPool will be treated as having higher permissions and should have access.

Here's a series of references for where other users have had this error, 'solved' several years ago:

https://github.com/Azure/app-service-announcements-discussions/issues/9
https://github.com/dotnet/cli/issues/5262
https://github.com/dotnet/corefx/issues/5252
https://github.com/dotnet/corefx/issues/30518

All of these reference the version of SQLClient (while this is relevant, it's a legacy issue), but one smart guy @azisoft (this link) realised this and has helped many thanks to it.

like image 41
Bradley Barfoot Avatar answered Oct 06 '22 16:10

Bradley Barfoot


For Entity Framework Core 3.x, install the Microsoft.Data.SqlClient Nuget package instead of System.Data.SqlClient.

like image 23
jsabrooke Avatar answered Oct 06 '22 16:10

jsabrooke