views:

81

answers:

2

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.

A: 

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.

Rob Levine
How do I check the ILMerge version? I tried `-v`, `-V`, `--version`, `-h`, but no luck. The only documents in C:\Program Files (x86)\Microsoft\ILMerge are ILMerge License.rtf and ILMerge.doc, neither of which seemed to contain a version. They look to have been modified in 2009. I'll try ILDASM on my merged DLL, thanks.
Sarah Vessels
This is my first time using ILDASM. Should it choke and display some error message somewhere? I'm not seeing anything awry when I open my merged DLL in it, just the namespace hierarchy.
Sarah Vessels
@Sarah - it doesn't choke itself, but the IL it generates has errors in it such as [SIGNATURE ENDED PREMATURELY], and IIRC an error about "type not found". It was the presence of these errors that let us work out which bit of code it was having a problem with. It turned out, in our case, to be problems with lambda expressions which (eventually) led us towards the version of ILMerge predating .Net 3.5. Definitely worth making sure you are using the most recent ILMerge I'd say, just to be sure.
Rob Levine
A: 

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.

Sarah Vessels