简体   繁体   English

Assembly.LoadFrom 无法加载 DLL

[英]Assembly.LoadFrom fails to load DLL

I have a program that loads plugins at runtime.我有一个在运行时加载插件的程序。 I iterate through each directory in the "Plugins" directory and attempt to load the DLL found.我遍历“插件”目录中的每个目录并尝试加载找到的 DLL。 Each plugin has its own directory with the plugin DLL file and another directory called "Dependencies".每个插件都有自己的包含插件 DLL 文件的目录和另一个名为“Dependencies”的目录。 If the plugin's DLL invokes the AssemblyResolve event I do the following:如果插件的 DLL 调用 AssemblyResolve 事件,我将执行以下操作:

  • Check if assembly is already in current AppDomain and return it if it is检查程序集是否已经在当前 AppDomain 中,如果是则返回
  • Check the plugin's Dependencies directory for the DLL and attempt Assembly.LoadFrom检查插件的 DLL 的依赖项目录并尝试 Assembly.LoadFrom
  • If no condition was met or last LoadFrom call fails just return null如果不满足任何条件或上次 LoadFrom 调用失败,则返回 null

Plugins that have no 3rd party dependencies or simple ones like Newtonsoft.Json or NHibernate load just fine.没有第 3 方依赖项或简单的插件(如 Newtonsoft.Json 或 NHibernate)加载就好了。 I have one plugin that depends on a DLL we'll just call "custom_library.dll."我有一个依赖于 DLL 的插件,我们将其称为“custom_library.dll”。 When the AssemblyResolve event is fired looking for this dependency I can confirm the file is in the Dependencies directory like it should be and even a File.Exists() call returns true.当触发 AssemblyResolve 事件以查找此依赖项时,我可以确认该文件在 Dependencies 目录中,就像它应该的那样,甚至 File.Exists() 调用也返回 true。

Unfortunately the Assembly.LoadFrom() call for this DLL throws a FileLoadException with the message "Could not load file or assembly 'custom_library.dll, Version=5.3.136.0, Culture=neutral, PublicKeyToken=null'. Nothing else is provided in the exception beyond HRESULT -2146233079. I can also reference this DLL through Visual Studio without problem.不幸的是,对这个 DLL 的 Assembly.LoadFrom() 调用抛出一个 FileLoadException 并带有消息“无法加载文件或程序集‘custom_library.dll,版本=5.3.136.0,Culture=neutral,PublicKeyToken=null’。在超出 HRESULT -2146233079 的异常。我也可以毫无问题地通过 Visual Studio 引用此 DLL。

Why am I getting this exception when trying to load the DLL at runtime instead of through Visual Studio's Add Reference feature?为什么在运行时而不是通过 Visual Studio 的添加引用功能加载 DLL 时会出现此异常?

Assembly.LoadFrom was failing because the 3rd party DLL that I was trying to reference was built as a .NET Framework project and I was in a .NET Core project. Assembly.LoadFrom 失败,因为我尝试引用的第 3 方 DLL 是作为 .NET Framework 项目构建的,而我在 .NET Core 项目中。 I confirmed this by creating my own DLL with one class as a .NET Framework Class Library project and received the same error.我通过使用一个类创建自己的 DLL 作为 .NET Framework 类库项目来确认这一点,并收到了相同的错误。 Once I took the exact same thing and built it targeting .NET Standard 2.0 everything loaded fine.一旦我采用完全相同的方法并针对 .NET Standard 2.0 构建它,一切都可以正常加载。

I spoke with the vendor and once they provided a different DLL that targeted .NET Standard the DLL was able to load and find all of its dependencies without issue.我与供应商进行了交谈,一旦他们提供了针对 .NET Standard 的不同 DLL,该 DLL 就能够毫无问题地加载并找到其所有依赖项。

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

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