gpt4 book ai didi

.net - GAC – 程序集在 GAC 中,但 “Could not load file or assembly”

转载 作者:行者123 更新时间:2023-12-04 15:04:37 25 4
gpt4 key购买 nike

我有一个由 Visual Studio 为我在 .NET dll 中使用的第三方 COM 对象生成的 Interop dll。

我已经在 GAC 中注册了我的消费 dll 和 Interop dll。我必须使用 GAC,因为 SharePoint 2010 工作流正在使用这些 DLL。

当执行到达我的 dll 调用 Interop dll 的点时,抛出以下错误“无法加载文件或程序集”……“系统找不到指定的文件”以及预期的版本和公钥。

如果我检查 Fusion Assembly Binding Log Viewer,Interop dll 的日志条目中会列出以下错误:

LOG: GAC Lookup was unsuccessful.

我可以在 GAC 中看到该程序集,并且它具有 FileNotFound 异常中指定的正确版本和公钥标记。

这是怎么回事?

最佳答案

问题是,除了版本和公钥有效之外,处理器架构 Interop 必须与您的调用 DLL 相匹配。而且,当然,目标框架必须是相同的版本。

我的 dll 被编译到 MSIL(Agnostic,AnyCPU)处理器,但出于某种奇怪的原因,Visual Studio 坚持将 Interop 编译为 x386。

(也许这通常不会导致问题,但我的 SharePoint 服务器是 64 位,这可能导致出现症状)。

解决方案是自己编译Interop:

1 - 从 GAC 注销您的 dll 和互操作。

2 - 启动 Visual Studio 命令提示符 与 .NET 框架相关的 Visual Studio 版本的 dll 目标 (即我正在 Visual Studio 2010 中开发我的 dll,目标是 .NET 3.5。要执行此步骤,我需要启动 Visual Studio 2008 命令提示符)

3 - 使用 Tlbmp 生成互操作

tlbimp <full path and filename of  COM .tlb>  /out:c:\.\Interop.CoolThirdParty.dll /keyfile: <full path and filename of snk> /machine:Agnostic /Namespace:CoolThirdParty  

/machine:Agnostic 导致针对 MSIL 处理器体系结构构建互操作,这与我的 dll 相同。

4 - 删除 dll 项目中对 Interop 的旧引用

5 - 删除旧的 Interop 文件并将其替换为您刚刚生成的文件(例如 c:.\Interop.CoolThirdParty.dll)

6 - 在您的项目中添加对新互操作的引用。

7 - 重建

8 - 在 GAC 中注册新构建的 dll 和新的互操作

它应该开始工作。

关于.net - GAC – 程序集在 GAC 中,但 “Could not load file or assembly”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3394106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com