c# - 尽管已加载但未找到另一个 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.
解决方案
您缺少的可能是缺少 B.dll 的另一个依赖关系吧。当您尝试加载 dll 时,Windows 将尝试查看它的所有依赖项(其他 dll)。不幸的是,您看到的错误将是:
“系统找不到指定的”
B.dll 的所有依赖项也应该复制到本地目录。
使用Dependency Walker .NET查找缺失的依赖项。
推荐阅读
- javascript - 字符串类型的 Mongoose _id 不抛出错误
- c# - ASP.NET 应用程序运行时如何将数据保存在内存(会话)中?
- ios - 如果集合中只有 1 个元素,则 UICollectionView 不进入 cellForItemAt 方法
- azure - 将自定义 exe 上传到 Azure Devops 管道
- regex - 如何将多个正则表达式字符串合并为一个?
- css - 引导下拉菜单类的问题
- javascript - Gatsby-Redux:组件不会在状态更改时重新渲染
- eclipse - 在 Eclipse 中打开一个新的内部 Web 浏览器选项卡?
- if-statement - 如何使用 query() 在 googlesheets 中构建搜索表单?
- arrays - Swift 中最大的数组(整数)容量是多少?