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, TaskCompletionSource
1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource
1 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](Func
2 operation, Func2 verifySucceeded, TState state) at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3
1.MoveNext() at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__301.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable
1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass20_01.<CompileQueryCore>b__0(QueryContext qc) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable
1 source)
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.
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.
For Entity Framework Core 3.x, install the Microsoft.Data.SqlClient
Nuget package instead of System.Data.SqlClient
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With