首页 > 解决方案 > 尽管已加载但未找到另一个 DLL 的 DLL 依赖项

问题描述

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

“无法加载文件或程序集 'B, Version=1.0.0.0, Culture=neutral, 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.

标签: c#reflection

解决方案


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

“系统找不到指定的”

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

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


推荐阅读