tags:

views:

32

answers:

2

I just upgraded a project from VS2008/.NET 3.5/PostSharp 1.5 to VS2010/.NET4.0/PostSharp 2.0.

Now, when running the unit tests for the system I get hundereds exceptions in the form of:

System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) at COMPANY.Data.NHibernate.BaseRepository.c__Binding`1.Invoke(Object& instance, Arguments arguments, Object aspectArgs) at PostSharp.Aspects.Internals.MethodInterceptionArgsImpl.Proceed() at COMPANY.Aop.TransactionAspectAttribute.OnInvoke(MethodInterceptionArgs context) in C:\COMPANY\Code\COMPANY-NET4.0\Core\Aop\TransactionAspectAttribute.cs:line 68 at COMPANY.Data.NHibernate.BaseRepository.Save[T](T scoreBigModel) in C:\COMPANY\Code\COMPANY-NET4.0\Core\DataAccess\NHibernate\BaseRepository.cs:line 102 at COMPANY.UnitTests.DataAccess.NHibernate.when_saving_a_canonical_term.<.ctor>b__5() in C:\COMPANY\Code\COMPANY-NET4.0\UnitTests\DataAccess\NHibernate\CanonicalTermRepositorySpecs.cs:line 29 at Machine.Specifications.Utility.RandomExtensionMethods.InvokeIfNotNull(Because because) at Machine.Specifications.Model.Context.EstablishContext()

When I run peverify.exe on the assembly, I see hundreds of errors in the following form. Always seems to be issues with generic methods:

[IL]: Error: [C:\COMPANY\Code\COMPANY-NET4.0\Core\bin\Debug\COMPANY.Core.dll : COMPANY.Data.NHibernate.ActivationRepository+c__Binding::Invoke][offset 0x0000008D][found ref 'PostSharp.Aspects.Internals.MethodBinding'][expected ref 'PostSharp.Aspects.Internals.MethodBinding`1[COMPANY.Models.Activation]'] Unexpected type on the stack.

[IL]: Error: [C:\COMPANY\Code\COMPANY-NET4.0\Core\bin\Debug\COMPANY.Core.dll : COMPANY.Data.NHibernate.ActivationRepository+c__Binding::Invoke][offset 0x00000056][found ref 'PostSharp.Aspects.Internals.MethodBinding`1[COMPANY.Models.Activation]'][expected ref 'PostSharp.Aspects.Internals.MethodBinding'] Unexpected type on the stack.

I am running the latest release of PostSharp 2.0 RC.

A: 

"System.BadImageFormatException" usually indicates a 64-bit/32-bit problem.

If you compile your code for "Any CPU" and run it on a 64-bit processor it will be JIT compiled to 64 bit. If it then calls any code (e.g. in an unmanaged dll) that is 32-bit, you'll get this exception when it tries to jump from 64-bit to 32-bit code.

If you're running on a 64-bit OS it's therefore possible that something in your upgrade has caused your program to get a mixture of 32-bit and 64-bit code in it. If you're running on a 32-bit OS then then can't be the problem, though, in which case it may indicate a corrupted binary.

If it is 32/64, then you can do the following: - Make sure all the dlls you use are the same bit-ness as your app, or - If you can't replace some 32-bit dlls with 64-bit versions, try compilng your app as "x86" rather than "Any CPU". This will force it to be compiled to 32-bit code even on a 64-bit PC, which will mean it has to run under WoW64 as a 32-bit app but it will be compatible with its 32-bit dlls.

Jason Williams
I tried compiling it as x86 and the runtime and peverify.exe errors did not change.It looks like there is a specific error with how PostSharp is rewriting the assembly -- pushing and popping an object but referring to it as generic at one time and non-generic at another time.
Mike
@Mike: I may not be much help than I'm afraid. I'd probably try rolling back to the previous PostSharp to see if I could isolate which part of the toolchain is responsible for the problem. (I'd be less inclined to suspect VS/.net in the first instance)
Jason Williams
+1  A: 

The issue is being adressed here: http://www.sharpcrafters.com/forum/Topic4896-19-1.aspx

Gael Fraiteur