views:

1037

answers:

3

While debugging through a .NET 3.5 SP1 project which is contacting a local web service, I'm receiving the exception

System.BadImageFormatException: "Bad Class Token"

Of course there aren't much more details about what's causing the exception.

I can tell that the method where this occurs, which is in the same class as it's caller, the debugger fails to reach. This exception occurs on the call of the method that contacts the web service. I do have other methods communicating with the web service, so the reference is good.

My unit tests for the method are also failing with the same exception.
Correction: my unit tests for the method are successful, furthering the confusion.

Does anyone know of a way to track down this exception? I've read through the documentation on the exception class, which leads me to believe that one of the assemblies is incorrect in its version, or there's an issue with the build.

What other steps would you suggest in troubleshooting this exception?

+3  A: 

Running peverify.exe on the assembly may yield useful diagnostics.

http://msdn.microsoft.com/en-us/library/62bwd2yd.aspx

Brian
Thank you for this suggestion, I have run the peverify.exe utility on all of the assemblies without any luck of finding the cause of this exception. But you did lead me to check the bitness of the assemblies with corflags and check through the IL, which I had not throught of before. Thanks!
Jeff Schumacher
+1  A: 

It turns out that I was getting this exception due to some abnormality in MSTest. Switched over to xUnit.net and everything has been fine since.

Jeff Schumacher
A: 

I have the same problem. The error "Test method SharePointServiceTest5.UnitTest1.SearchBeta threw exception: System.BadImageFormatException: Could not load file or assembly 'Microsoft.Office.Server.Search, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c' or one of its dependencies. An attempt was made to load a program with an incorrect format.."

My code references Microsoft.Office.Server, Microsoft.Office.Server.Search.

Not sure why/where the conflict but this error is preventing me from unit testing a service against SharePoint. Arrgh

David
Ensure the Target Platform matches. x86 should be used if you ever reference 32bit native DLLs.
sixlettervariables
@sixlettervariables I was going to say the same thing. :)
Jeff Schumacher