Problems using PostSharp with ILMerge

303 views Asked by At

I am creating an application that I want to ship as a single exe, or as near to as possible. Initially I tried bundling all dependencies using ILMerge, but have discovered this causes PostSharp to fail with the following exception:

System.TypeInitializationException: The type initializer for 'TestClass`1' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'PostSharp.ImplementationDetails_5c83534c.<>z__a_1a' threw an exception.
 ---> System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)
    at PostSharp.ImplementationDetails_5c83534c.<>z__a_1a..cctor()
    --- End of inner exception stack trace ---
    at PostSharp.ImplementationDetails_5c83534c.<>z__a_1a.Initialize()
    at TestClass`1..cctor() in :line 0

I found this blog post from 2008 on problems using PostSharp with ILMerge, but they seem to centre around a use case which has multiple libraries that use PostSharp being merged with the main exe. My case is much simpler:

I have a single exe that uses PostSharp, plus some third party libraries that don't use PostSharp. The resulting exe has the same name as the original one, so I believe PostSharp should be able to find the classes.

The minimal method that reproduces the problem is to just ILMerge the single exe and leave the rest of the dependencies as separate dll files. In this toy version, ILMerge is pointless - I'm just running it over the one exe. However, this is enough to reproduce the problem.

If instead of running ILMerge I just leave the original exe alone, the exception is not thrown. So clearly ILMerge is interfering with PostSharp somehow. Here is the command line my build process is using:

 ilmerge /out:Releases/Prowl-a6/Prowl.exe "/lib:C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5" "/targetplatform:v4,C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.5" D:\Dropbox\Code\C#\Prowl\Prowl.exe

I am using PostSharp in just one place: I have an abstract attribute class that implements MethodInterceptionAspect, and then two concrete classes that extend it.

Any ideas as to what I could do to make this work?

EDIT: After some investigation, I have discovered the error only occurs when instantiating a generic class which contains one of the MethodInterceptionAspects. Non-generic ones are fine. Here is a minimal solution that demonstrates the error.

1

There are 1 answers

0
Martin Eden On BEST ANSWER

As suggested by @stakx, I went with an alternative to ILMerge. It's now working nicely with LibZ.