简体   繁体   English

'无法加载文件或程序集 'netstandard,版本 = 2.0.0.0,...'。 不应加载引用程序集以执行

[英]'Could not load file or assembly 'netstandard, Version=2.0.0.0, ...'. Reference assemblies should not be loaded for execution

Goal: From a .NET 4.7 console app, using reflection with Assembly.GetType(), I am trying extract the Type of a netstandard 2.0 class from Assembly X. Then I want to create an instance of this Type with Activator.CreateInstance().目标:从 .NET 4.7 控制台应用程序,通过 Assembly.GetType() 使用反射,我尝试从 Assembly X 中提取 netstandard 2.0 类的类型。然后我想使用 Activator.CreateInstance() 创建此类型的实例.

What I am trying to do: However, this assembly X has a dependency to netstandard 2.0.我想做的是:但是,这个程序集 X 依赖于 netstandard 2.0。 To be able to get the Type, netstandard dependency has to be loaded into the AppDomain.为了能够获取类型,必须将 netstandard 依赖项加载到 AppDomain 中。 That's why when the AppDomain is requesting the netstandard assembly through the AssemblyResolve event, I simply load the dll like this:这就是为什么当 AppDomain 通过 AssemblyResolve 事件请求 netstandard 程序集时,我只是像这样加载 dll:

var netStandardDllPath = @"C:\Users\xxx\.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll";

return Assembly.LoadFrom(netStandardDllPath);

Which throws:哪个抛出:

System.BadImageFormatException: 'Could not load file or assembly 'file:///C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ref\netstandard.dll' or one of its dependencies. System.BadImageFormatException:“无法加载文件或程序集”file:///C:\Users\vincent.lerouvillois.nuget\packages\NETStandard.Library.2.0.0-preview1-25301-01\build\netstandard2.0\ ref\netstandard.dll' 或其依赖项之一。 Reference assemblies should not be loaded for execution.不应为执行加载引用程序集。 They can only be loaded in the Reflection-only loader context.它们只能在仅反射加载器上下文中加载。 (Exception from HRESULT: 0x80131058)' (HRESULT 异常:0x80131058)'

Inner Exception: BadImageFormatException: Cannot load a reference assembly for execution.内部异常:BadImageFormatException:无法加载要执行的引用程序集。

What I know: I know that they want us to load the DLL with Assembly.ReflectionOnlyLoadFrom.我所知道的:我知道他们希望我们使用 Assembly.ReflectionOnlyLoadFrom 加载 DLL。 But doing that will prevent me from instanciate the type with Activator.CreateInstance().但这样做会阻止我使用 Activator.CreateInstance() 实例化类型。 See Microsoft official post微软官方帖子

Also, I tried referencing the Nuget packages NETStandard.Library 2.0.0-preview1-25301-01 and NETStandard.Library.NETFramework 2.0.0-preview1-25305-02 in my console app so it would have the netstandard 2.0 libraries referenced, but it didn't change anything.另外,我尝试在我的控制台应用程序中引用 Nuget 包 NETStandard.Library 2.0.0-preview1-25301-01 和 NETStandard.Library.NETFramework 2.0.0-preview1-25305-02,这样它就会引用 netstandard 2.0 库,但是它没有改变任何东西。

Question: Does anyone would know if there is a proper way to load that dll without error, or maybe if this is a bug, or else?问题:有没有人知道是否有正确的方法来无错误地加载该 dll,或者这是否是一个错误,或者其他? Or why this kind of dll is not able to load for execution?或者为什么这种dll无法加载执行?

The netstandard.dll you are trying to load is a reference assembly that which cannot be loaded for runtime on .NET Framework as pointed out by others.您尝试加载的 netstandard.dll 是一个参考程序集,正如其他人所指出的那样,无法在 .NET Framework 上为运行时加载该程序集。 However if you need to resolve that dependency you will need to runtime version that maps to the framework you are trying to run on.但是,如果您需要解决该依赖关系,您将需要映射到您尝试运行的框架的运行时版本。

For .NET Standard support we are including them as part of the msbuild extensions that ship with VS so you will want to get the version of netstandard.dll from there.对于 .NET Standard 支持,我们将它们作为 VS 附带的 msbuild 扩展的一部分包含在内,因此您需要从那里获取 netstandard.dll 的版本。 Depending on which version of VS2017 you have installed it should be somewhere like C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll or from the .NET Core 2.0 SDK you can find it C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll根据您安装的 VS2017 版本,它应该位于C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll或者从 .NET Core 2.0 SDK 你可以找到它C:\Program Files\dotnet\sdk\2.0.0\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\netstandard.dll

Try using one of those versions in your scenario.尝试在您的场景中使用其中一个版本。

Wow.哇。 I just spent several hours tracking the cause of this "could not load... netstandard" error down.我只是花了几个小时来跟踪这个“无法加载... netstandard”错误的原因。

For me, the problem was that my .NET Framework project (which references both .NET Framework and .NET Standard libraries) was built with .NET Framework 4.7.2 and the system where I was deploying and running it did not have 4.7.2 installed.对我来说,问题在于我的 .NET Framework 项目(引用 .NET Framework 和 .NET Standard 库)是使用 .NET Framework 4.7.2 构建的,而我部署和运行它的系统没有 4.7.2安装。

Deploying a very small Console project with the same basic structure and references and executing that in a Command window finally revealed the correct error, in a pop-up, that .NET Framework 4.7.2 was missing.部署一个具有相同基本结构和引用的非常小的控制台项目,并在命令窗口中执行该项目,最终在弹出窗口中显示了正确的错误,即缺少 .NET Framework 4.7.2。

If you're struggling with this particular error, make sure you have the necessary .NET Framework installed.如果您遇到这个特定错误,请确保安装了必要的 .NET Framework。

Set Copy Local to true in netstandard.dll properties.在 netstandard.dll 属性中将Copy Local设置为 true。

  1. Open Solution Explorer and right click on netstandard.dll.打开解决方案资源管理器并右键单击 netstandard.dll。
  2. Set Copy Local to true.Copy Local设置为 true。

在此处输入图像描述

You can't load a reference assembly.您无法加载参考程序集。

.NET Standard is a collection of APIs that must be provided by .NET Standard compatible implementations. .NET Standard 是 API 的集合,必须由 .NET Standard 兼容实现提供。

A reference assembly only contains contracts.参考程序集仅包含合同。 This means that it contains no implementation.这意味着它不包含任何实现。 The assembly you are trying to load contains the .NET Standard 2.0 contracts.您尝试加载的程序集包含 .NET Standard 2.0 协定。

A contract looks like this: https://github.com/dotnet/standard/blob/master/netstandard/ref/mscorlib.cs合同如下所示: https ://github.com/dotnet/standard/blob/master/netstandard/ref/mscorlib.cs

EDIT: .NET Framework 4.7 implements .NET Standard 2.0, so you shouldn't need to load any assembly to use Activator.CreateInstance() to instantiate a .NET Standard type.编辑: .NET Framework 4.7 实现 .NET Standard 2.0,因此您无需加载任何程序集即可使用Activator.CreateInstance()实例化 .NET Standard 类型。

NETStandard 2.0.0-preview1 in not compatibility with net461 and net47. NETStandard 2.0.0-preview1 与 net461 和 net47 不兼容。

but for realese .NET Core SDK 2.0 assemblies (as well as 2.0.0-preview2)但对于 realese .NET Core SDK 2.0 程序集(以及 2.0.0-preview2)

 var netStandardDllPath = @"c:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.netcore.app\2.0.0\ref\netcoreapp2.0\netstandard.dll";
 Console.WriteLine(Assembly.LoadFrom(netStandardDllPath).FullName);

all is ok.一切都好。

But if you steel need to load preview1 libraries, maybe you should to use netstandard2.0 instead net471.但是如果你需要加载 preview1 库,也许你应该使用 netstandard2.0 而不是 net471。

For me solved doing the following: 1 - Installed latest.Net Framework on server.对我来说解决了以下问题: 1 - 在服务器上安装了 latest.Net Framework。 2 - Updated windows server and my local machine. 2 - 更新了 windows 服务器和我的本地机器。 3 - Went to Manage Nuget Package and updated all references on the update tab. 3 - 去管理 Nuget 包并更新更新选项卡上的所有引用。

Perhaps only doing step 3 can solve in your case也许只做第 3 步就可以解决你的问题

In case if IBM Message Queue references are used in the project solution, this exception indicates that the DLL used for refering MQ classes are incompatible with the host(server) .NET version installed.如果项目解决方案中使用了IBM Message Queue引用,则此异常表明用于引用 MQ 类的 DLL 与安装的主机(服务器).NET 版本不兼容。

In this scenario, either we need to update server with latest update and make sure .NET latest version is available or use lower version of IBM Message queue DLL as reference.在这种情况下,我们要么需要使用最新更新更新服务器并确保 .NET 最新版本可用,要么使用较低版本的 IBM Message queue DLL 作为参考。

Old version DLL - amqmdnet.dll (no new features will be introduced by IBM as not in support)旧版本 DLL - amqmdnet.dll (IBM 不会引入新功能,因为不支持)

Latest version DLL - amqmdnetstd.dll (to run IBM MQ classes for .NET Standard, you must install Microsoft .NET Core)最新版本 DLL - amqmdnetstd.dll (要运行适用于 .NET Standard 的 IBM MQ 类,您必须安装 Microsoft .NET Core)

Install NetStandard.Library 2.0.0.0 from NuGet, It works for me.从 NuGet 安装 NetStandard.Library 2.0.0.0,它对我有用。 when I downgrade .net framework 4.6.1 to 4.6.0当我将 .net framework 4.6.1 降级到 4.6.0 时

If you are having this issue for a project that used to work, try deleting the bin and obj folders since caching can cause this, too.如果您的项目曾经有效,请尝试删除binobj文件夹,因为缓存也可能导致此问题。

暂无
暂无

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

相关问题 .NET 核心与 .NET 框架 - 无法加载文件或程序集'System.ServiceModel,不应加载参考程序集以执行 - .NET Core vs .NET Framework - Could not load file or assembly 'System.ServiceModel, Reference assemblies should not be loaded for execution 无法加载文件或程序集 Microsoft.VisualStudio.Shell,版本 = 2.0.0.0 - Could not load file or assembly Microsoft.VisualStudio.Shell, Version=2.0.0.0 当引用的 dll 文件位于加载的程序集中时,“无法加载文件或程序集” - "Could not load file or assembly" when the referenced dll file is in the loaded assemblies 无法加载文件或程序集Microsoft.Owin.Security.OAuth,Version = 2.0.0.0 - Could not load file or assembly Microsoft.Owin.Security.OAuth, Version=2.0.0.0 无法加载文件或程序集'System.Web.WebPages.Razor,版本= 2.0.0.0,win8 - Could not load file or assembly 'System.Web.WebPages.Razor, Version=2.0.0.0, win8 无法加载文件或程序集'Microsoft.Azure.CosmosDB.BulkImport,版本= 2.0.0.0 - Could not load file or assembly 'Microsoft.Azure.CosmosDB.BulkImport, Version=2.0.0.0 无法加载文件或程序集'System.Data version = 2.0.0.0'或其依赖项之一 - Could not load file or assembly 'System.Data version=2.0.0.0' or one of its dependencies NuGet 更新,现在抛出 OpenTok:System.IO.FileLoadException:无法在 CreateSession 上加载文件或程序集 JWT 版本 = 2.0.0.0 - NuGet update, now throwing OpenTok: System.IO.FileLoadException: Could not load file or assembly JWT Version=2.0.0.0 on CreateSession 无法加载文件或程序集,但它们已加载 - Could not load file or assembly but they are loaded 添加迁移时无法加载文件或程序集网络标准 - Could not load file or assembly netstandard when I add migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM