PostSharp causes BadImageException when applied to generic methods -- errors when running peverify.exe

0

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.

c#
postsharp
asked on Stack Overflow Jul 2, 2010 by Mike • edited Jul 2, 2010 by Mike

2 Answers

1

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

answered on Stack Overflow Jul 2, 2010 by Gael Fraiteur
0

"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.

answered on Stack Overflow Jul 2, 2010 by Jason Williams

User contributions licensed under CC BY-SA 3.0