【发布时间】:2012-02-20 12:46:09
【问题描述】:
随着迁移到 .net 4,我们开始面临库的问题。 假设我们有我们的库 MyLib.dll,它引用互操作程序集 Interop.dll。 Interop.dll 引用了 MissingInterop.dll。
所以引用可以显示为:MyLib.dll -> Interop.dll -> MissingInterop.dll
在 MyLib.dll 中,我们仅使用 Interop.dll 中的部分类,因此我们从不调用任何需要 MissingInterop.dll 的东西,而在 .net 3.5 中它工作得很好这就是我们不使用 MyLib.dll 发布 MissingInterop.dll 的原因.
当我们从在 .net 4 下运行的进程中使用 MyLib.dll 时,应用程序失败并出现以下异常:
FileNotFoundException:“无法加载文件或程序集 'MissingInterop.dll, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' 或其依赖项之一。系统找不到指定的文件。”`
我还注意到 Interop.dll 引用了其他丢失的 dll,但 .net 不会尝试加载它们。我发现了不同之处。 MissingInterop.dll 中的某些类型用于 Interop.dll 中的方法参数,而其他缺失库中的类型仅用作方法返回类型,即在 Interop.dll 中我可以看到:
Class C1
MethodA : void (valuetype [MissingInterop]MissingAssembly.TypeA&)
Class C2
get_Status : valuetype[AnotherMissingInterop]AnotherMissingAssembly.TypeB()
而 .NET 4 会尝试加载 MissingInterop.dll,但不会尝试加载 AnotherMissingInterop.dll。
.NET 3.5 没有尝试加载 MissingInterop.dll 和 AnotherMissingInterop,因为它们没有在执行路径中使用。
我知道 .NET 4 有新的 Fusion,但我还没有在任何地方发现这样的重大变化。 有人知道为什么 .NET 4 会尝试加载不需要的东西吗? 有没有办法在不重新编译代码或添加丢失文件的情况下解决这个问题?
【问题讨论】:
-
是的,在 .NET 4 中进行了重大更改以启用“嵌入互操作类型”功能。你之前打破了保修,它意外地起作用了。使用 [ComImport] 提供缺少的类型。
-
可能是因为其中一个参考是针对框架 3.5 构建的。您应该尝试添加 useLegacyV2RuntimeActivationPolicy="true",如下所述:stackoverflow.com/questions/1604663/…
-
Benoit,感谢您的建议,但没有帮助。
-
据我所知,clr 4 与之前的版本有很多不同。如果你可以不断地复制它,那么它就是设计使然。由于修复简单明了,我认为微软不应该修复它。
标签: .net exception-handling assembly-resolution assemblyresolve