GAC - 程序集在GAC中,但"无法加载文件或程序集"

Mic*_*ues 6 .net com interop gac

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

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

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

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

LOG: GAC Lookup was unsuccessful.
Run Code Online (Sandbox Code Playgroud)

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

这是怎么回事?

Mic*_*ues 4

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

\n\n

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

\n\n

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

\n\n

解决方案是自己编译Interop:

\n\n

1 - 从 GAC 取消注册您的 dll 和 Interop。

\n\n

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

\n\n

3 - 使用 Tlbmp 生成互操作

\n\n
tlbimp <full path and filename of  COM .tlb>  /out:c:\\.\\Interop.CoolThirdParty.dll /keyfile: <full path and filename of snk> /machine:Agnostic /Namespace:CoolThirdParty  \n
Run Code Online (Sandbox Code Playgroud)\n\n

/ machine :Agnostic 会导致针对 MSIL 处理器架构构建 Interop,这与我的 dll 相同。

\n\n

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

\n\n

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

\n\n

6 - 将项目中的引用添加到新的 Interop。

\n\n

7 - 重建

\n\n

8 - 在 GAC 中注册新构建的 dll 和新的 Interop

\n\n

应该开始工作。

\n