Windows 7 64 中的 .NET ILMerged DLL 出现 BadImageFormatException

发布于 2024-09-10 08:24:23 字数 1014 浏览 2 评论 0原文

我正在使用 Visual Studio 2008 在 Windows 7 64 位中进行开发。我有一组类库,我使用 ILMerge 将它们合并到单个 DLL 中。然而,当我尝试使用这个合并的 DLL 时,我得到了

[BadImageFormatException:无法加载文件或程序集“MyMergedDll”或其依赖项之一。尝试加载格式不正确的程序。]

我一直在寻找有关此问题的帮助,看起来我需要将 MyMergedDll 中每个项目的 Build 属性设置为目标 x86,所以我这样做了。我还在 Web 服务中设置了所有非 MSTest 项目,在其中引用 MyMergedDll 以定位 x86。但是,我仍然收到此错误。

如果此博客条目值得相信的是,我可以因为这样做而得到这个错误:

public class SpecificClass: BaseClass: where T : class { }

也就是说,对泛型有类约束。不过,该博客条目是 2007 年的,所以我不知道这是否仍然适用。在我跟踪项目中的每个泛型类以检查约束之前,我希望从 StackOverflow 获得一些关于你们认为问题所在的信息。

如果重要的话,MyMergedDll 是强名称签名的。我正在尝试在控制台应用程序和 Web 服务中使用 MyMergedDll。我不知道每次重建 Web 服务时 IIS 是否都会更新,事情是否会变得复杂。对于控制台应用程序,当我在发布模式下构建时,我似乎只会收到 BadImageFormatException

I'm developing in Windows 7 64-bit with Visual Studio 2008. I have a collection of class libraries that I merge into a single DLL using ILMerge. When I try to use this merged DLL, however, I get

[BadImageFormatException: Could not load file or assembly 'MyMergedDll' or one of its dependencies. An attempt was made to load a program with an incorrect format.]

I've been searching around for help with this and it looked like I needed to set the Build property in each project in MyMergedDll to target x86, so I did that. I also set all non-MSTest projects in the web service in which I'm referencing MyMergedDll to target x86. I'm still getting this error, however.

If this blog entry is to be believed, I can get this error as a result of doing:

public class SpecificClass: BaseClass: where T : class { }

That is, having a class constraint on a generic. The blog entry is from 2007, though, so I don't know if that still applies. I wanted some input from StackOverflow as to what you guys think the problem is before I go tracking down every generic class in my projects to check constraints.

If it matters, MyMergedDll is strong-name signed. I'm trying to use MyMergedDll in both a console application and in a web service. I don't know if things are perhaps complicated by whether or not IIS is updating every time I rebuild the web service. With the console application, I only seem to get the BadImageFormatException when I build in Release mode.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心在旅行 2024-09-17 08:24:23

我让它工作起来,我认为这是通过做两件事来完成的:

  1. MyMergedDLL 是使用我可以控制的其他几个程序集构建的,但这些程序集没有设置为目标 x86。我重建了那些针对 x86 的程序集,重建了组成 MyMergedDLL 的程序集,并重新合并了这些程序集以形成 MyMergedDLL 的新副本。
  2. 使用 MyMergedDLL 的控制台应用程序在调试模式下设置为目标 x86,但不是发布模式,因此它可以在调试模式下工作,但在发布模式下给出 BadImageFormatException

第一件事可能没有必要。我想我只需要 MyMergedDLL 的使用者在发布模式下也以 x86 为目标。我永远无法弄清楚我使用的是哪个版本的 ILMerge,因此我只是使用我在 Microsoft 网站上找到的最新 MSI 重新安装了它。

I got it to work and I think it was from doing two things:

  1. MyMergedDLL was built using a couple of other assemblies I have control over, but those assemblies were not set to target x86. I rebuilt those assemblies targeting x86, rebuilt the assemblies that make up MyMergedDLL, and re-merged those assembles to form a new copy of MyMergedDLL.
  2. The console application that uses MyMergedDLL was set to target x86 in Debug mode but not Release mode, hence why it would work in Debug mode but give the BadImageFormatException in Release mode.

The first thing might not have been necessary. I think I just needed the consumer of MyMergedDLL to also target x86 in Release mode. I never could figure out which version of ILMerge I was using, so I just re-installed it with the latest MSI I could find on Microsoft's site.

记忆里有你的影子 2024-09-17 08:24:23

您是否仔细检查过您正在使用的 ILMerge 版本?

我问这个问题是因为旧的、不正确的 aspnet_merge 版本(它基本上对 dll 进行相同的合并)导致我们遇到了您所描述的相同问题。我在为其他人回答这个问题时详细介绍了一些细节就这样。

快速查看一下 ILDASM 中的 dll 可能是值得的(当尝试反编译这些坏的 dll 时,Reflector 往往会失败 - 也许并不奇怪),看看您是否可以找出损坏的点以及与代码中对应的内容:它可能会阐明问题。

Have you double-checked the version of ILMerge you are using?

I ask because an old, incorrect version of aspnet_merge (which basically does the same merging of dlls amongst other things) caused us to have the same problems you are describing. I went into some detail when answering this question for someone else on SO.

It may be worth having a quick look at the dll in ILDASM (Reflector tends to fall over when trying to decompile these bad dlls - perhaps unsuprisingly) and see if you can make out the point of corruption and what that corresponds to in your code as it may shed light on the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文