【问题标题】:DLL dependency of another DLL not found though loaded尽管已加载但未找到另一个 DLL 的 DLL 依赖项
【发布时间】:2019-12-16 02:10:16
【问题描述】:

在一个 C# 应用程序中,我正在加载用户在运行时提供的 2 个 DLL(不是预定义的引用)让它成为 A.dll、B.dll。 A.dll 引用 B.dll,但它们是单独提供的。 当我尝试使用 B.dll 中声明的类型参数从 A.dll 访问方法时,我得到:

“无法加载文件或程序集'B,版本=1.0.0.0,文化=中性, PublicKeyToken=null' 或其依赖项之一。系统无法 找到指定的文件。”。

错误,虽然两个 DLL 都已加载。即使调用MethodInfo.GetParameters() 也会抛出异常。

尝试使用不同的方法加载两个 DLL:

Assembly.Load(<Path>) 
Assembly.LoadFile(<Path>) 
Assembly.LoadFrom(<Path>)

我将项目 A 中对 B.dll 的引用设置为“Copy Local = false”。

任何不涉及上述连接的 A 或 B 的类型和方法的使用似乎都可以正常工作。

我确保两个程序集都加载了 AppDomain.CurrentDomain.GetAssemblies() (他们都是)。

我必须假设我没有开发人员访问 A.dll 和 B.dll 项目(由用户提供)的权限,但出于测试目的,我也可以尝试更改其中的内容。

A.dll 的融合日志:

*** Assembly Binder Log Entry  (8/10/2019 @ 11:47:04 PM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\Users\<username>\AppData\Local\JetBrains\Installations\ReSharperPlatformVs15_95cb26a8\JetBrains.ReSharper.TaskRunner.CLR45.x64.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = A, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///<project fullpath>/bin/Debug
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = C:\Users\<username>\AppData\Local\Temp\u5r0nb10.kwf\k2mn3yzp.umi
LOG: AppName = AppTest
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: No application configuration file found.
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///<project fullpath>/bin/Debug/A.DLL.
LOG: Attempting download of new URL file:///<project fullpath>/bin/Debug/A/A.DLL.
LOG: Attempting download of new URL file:///<project fullpath>/bin/Debug/A.EXE.
LOG: Attempting download of new URL file:///<project fullpath>/bin/Debug/A/A.EXE.
LOG: All probing URLs attempted and failed.

【问题讨论】:

  • A 是否针对与提供的 B 不同的版本编译?
  • 无论如何enable fusion logging 在加载程序集失败时查看实际错误。
  • A 与 B 的相同版本编译。
  • 融合日志作为编辑添加到原始帖子

标签: c# reflection


【解决方案1】:

你缺少的可能是缺少另一个B.dll的依赖吧。 当您尝试加载 dll 时,Windows 将尝试查看它的所有依赖项(其他 dll)。不幸的是,您看到的错误将是:

“系统找不到指定的”

B.dll的所有依赖也要复制到本地目录。

使用Dependency Walker .NET 查找缺失的依赖项。

【讨论】:

  • “来自 A 或 B [...] 的任何类型和方法的使用似乎都可以正常工作。”
  • 感谢您的快速回答。如果这是正确的,那么直接使用 B.dll 也会失败,不是吗?
  • 是的。我检查了,一切都在那里。
【解决方案2】:

既然一切都在“那里”,我就有了另一种可能性。通常情况下,这很模糊,但它可能会发生。

如果 B.dll 依赖于您的主程序所依赖的更高版本的 dll,有时您加载的较低版本的 dll 无法与 B 想要的更高版本一起使用。在这种情况下,您会收到此错误。

【讨论】:

    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 2018-11-30
    • 1970-01-01
    • 2016-12-02
    相关资源
    最近更新 更多