Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top