简体   繁体   English

无法加载 DLL 'sni.dll' - Entity Framework Core

[英]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'.在 Visual Studio 2017 上的 ASP.Net Core 应用程序中使用 Entity Framework Core 时,我间歇性地收到错误“无法加载‘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.但我肯定知道(因为调试捕获异常)当 EF 有原因从数据库获取数据并转换为实体时,它就会发生。

Thanks in advance for any suggestions.在此先感谢您的任何建议。

--EDIT This is still happening. --EDIT 这仍在发生。 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.发生 System.DllNotFoundException HResult=0x80131524 Message=无法加载 DLL 'sni.dll':指定的网络名称不再可用。 (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.Dat (来自 HRESULT 的异常:0x80070040)Source = StackTrace:在 System.Data.SqlClient.SNINativeMethodWrapper.UnmanagedIsTokenRestricted(IntPtr 令牌,Boolean&isRestricted)在 System.Data.Win32NativeMethods.IsTokenRestrictedWrapper(IntPtr 令牌)在 System.Data.ProviderBase.DbConnectionPoolIdentity.Get () 在 System.Data.ProviderBase.DbConnectionPoolGroup.GetConnectionPool( 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource连接工厂) 1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource 1 重试, DbConnectionOptions userOptions) 在 System.Dat a.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 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, Func 2 verifySucceeded, TState state) at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3 1.MoveNext() at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__30 1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable 1 source) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass20_0 1.<CompileQueryCore>b__0(QueryContext qc) at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable 1 source) a.SqlClient.SqlConnection.TryOpen(TaskCompletionSource 1 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 操作,Func 2 verifySucceeded, TState state) at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3 1.MoveNext( ) 在 Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_Include>d__30 1.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable 1 来源)在 Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass20_0 1.<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.添加 nuget 包 System.Data.SqlClient v4.4.0 为我解决了同样的问题。 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.这是由于旧版本的 SqlClient 依赖于 dotnet Core 2 中不可用的 API。

In some instances this issue can be caused by IIS not having sufficient permissions to access areas of your %USERPROFILE%.nuget在某些情况下,此问题可能是由于 IIS 没有足够的权限来访问 %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).授予 IIS appPool(如 DefaultAppPool 或 FooAppPool)访问权限,不仅可以访问 .Nuget 文件夹(这通常是自动完成的)。

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.在 Identity 下将用于运行的 AppPool 设置为“LocalSystem”,这样 appPool 将被视为具有更高的权限并且应该具有访问权限。

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.所有这些都引用了 SQLClient 的版本(虽然这是相关的,但它是一个遗留问题),但是一个聪明的人@azisoft这个链接)意识到了这一点,并因此帮助了很多人。

For Entity Framework Core 3.x, install the Microsoft.Data.SqlClient Nuget package instead of System.Data.SqlClient .对于 Entity Framework Core 3.x,安装Microsoft.Data.SqlClient Nuget 包而不是System.Data.SqlClient

I had the same Problem with a .NET 4.8 WebApplication with MVC5 and WebApi that is using EF Core 3.15.对于使用 EF Core 3.15 的带有 MVC5 和 WebApi 的 .NET 4.8 WebApplication,我遇到了同样的问题。

Out code is seperated in several Projects.输出代码在几个项目中是分开的。 I only addeded the EF Core References to the Projects that needs to access DB.我只将 EF Core 引用添加到需要访问数据库的项目中。 This leads to the same Problem as you guys have here.这会导致与你们在这里遇到的问题相同。

After i added the EF Core References also to the startup Project it works as intended.在我也将 EF Core References 添加到启动项目后,它按预期工作。

Hope this helps someone.希望这对某人有帮助。

This is occurred due to System.Data.SqlClient.TO resolve that,这是由于 System.Data.SqlClient.TO 解决的,

1)Right click on your project and click on manage nuget package. 1)右键单击您的项目并单击管理nuget包。

2)install Microsoft.Data.SqlClient instaed of System.Data.SqlClient 2)安装 Microsoft.Data.SqlClient 代替 System.Data.SqlClient

3)rebuild project and publish 3)重建项目并发布

(I am using .net core 2.0 web api) (我正在使用 .net core 2.0 web api)

For me, a little modified nate's way, worked for me:对我来说,稍微修改一下 nate 的方式,对我有用:

<Import
   Project="$(NuGetPackageRoot)Microsoft.Data.SqlClient.SNI.1.1.0\build\net46\Microsoft.Data.SqlClient.SNI.targets"
   Condition="Exists('$(NuGetPackageRoot)Microsoft.Data.SqlClient.SNI.1.1.0\build\net46\Microsoft.Data.SqlClient.SNI.targets')" 
/>

If you are using source control where EFCore was newly added when pulling changes, try restore nuget packages and then close and reopen Visual Studio.如果您在拉取更改时使用新添加 EFCore 的源代码管理,请尝试恢复 nuget 包,然后关闭并重新打开 Visual Studio。

Our current guessing that this line in the project file wasn't executing correctly when the package was restored我们目前猜测项目文件中的这一行在恢复包时没有正确执行

<Import Project="..\..\packages\Microsoft.Data.SqlClient.SNI.1.1.0\build\net46\Microsoft.Data.SqlClient.SNI.targets" Condition="Exists('..\..\packages\Microsoft.Data.SqlClient.SNI.1.1.0\build\net46\Microsoft.Data.SqlClient.SNI.targets')" />

This seems like a silly answer but it just worked for us.这似乎是一个愚蠢的答案,但它对我们有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM