Sunday, August 28, 2011

System.BadImageFormatException: Could not load file or assembly <assembly> or one of its dependencies. An attempt was made to load a program with an incorrect format.

I stumbled on this issue today. I have two projects – one that was built for x64, and another one, a test project – built for x86. The test project has a project reference to the library. When I try to execute any of the unit tests, I’m getting the following error:

System.BadImageFormatException: Could not load file or assembly 'assembly name here, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

I searched online and found the following article, which opened my eyes how to fix the issue:

http://support.microsoft.com/kb/967163

It’s clear what is the issue – x86 assembly is trying to access x64 assembly. The question is how to solve that issue. The answer is – set the “Platform Target” setting in the x64 assembly project config to “Any CPU”:

image

No comments: