Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadImageFormatException when AnyCPU test assembly implements interface from x64 production assembly

I seem to have hit on a scenario where when I run mstest on an AnyCPU assembly which references an x64 assembly, I get a BadImageFormatException.

The issue occurs when an interface in x64Production.dll is implemented (even if unused) by the AnyCPUTestingx64Production.dll test assembly:

Unable to load the test container 'D:\AnyCPUTestingx64Production.dll' 
or one of its dependencies. error details:
System.BadImageFormatException: 
    Could not load file or assembly 'x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  • mstest is running on windows 7 64-bit
  • the test assembly is built as AnyCPU to get it to run in 64bit on a 64bit host (as outlined here)
  • the testsettings file specifies <Execution hostProcessPlatform="MSIL"/>
  • peverify and corflags reveal nothing interesting
  • this is readily reproducible in a toy solution, i.e. where
    • x64Production
      • references no other assemblies
      • includes only an empty public interface IExampleInterface
      • has <PlatformTarget> set to x64
    • AnyCPUTestingx64Production
      • references only x64Production.dll (i.e. this issue is present even without a reference to Microsoft.VisualStudio.QualityTools.UnitTestFramework)
      • includes only an empty implementation of x64Production.IExampleInterface
      • has <PlatformTarget> set to x64
  • nunit can load and run the test assembly (once I've converted all the test attributes)
    • but is not a good short term solution to the larger problem (which involves a huge number of project files)
  • the same issue arises whether the projects target 3.5 or 4.0
  • the same issues arises whether the VS2008 or VS2010 c# compiler is used
  • the same issue arises whether mstest from VS2010 or Test Agents is used
  • it is mstest which fails while loading AnyCPUTestingx64Production - i.e this is not an issue with attempting to load the assembly in the wrong QTAgent (nothing shows up in Process Monitor and renaming QTAgent32.exe has no effect):
    *** Assembly Binder Log Entry  (09/02/2012 @ 09:44:26) ***

    The operation failed.
    Bind result: hr = 0x8007000b. An attempt was made to load a program with an incorrect format.

    Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
    Running under executable  C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe
    --- A detailed error log follows. 

    === Pre-bind state information ===
    LOG: User = David
    LOG: DisplayName = x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
     (Fully-specified)
    LOG: Appbase = file:///D:/
    LOG: Initial PrivatePath = NULL
    LOG: Dynamic Base = NULL
    LOG: Cache Base = NULL
    LOG: AppName = MSTest.exe
    Calling assembly : AnyCPUTestingx64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
    ===
    LOG: This bind starts in default load context.
    LOG: Using application configuration file: C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe.Config
    LOG: Using host configuration file: 
    LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
    LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
    LOG: Attempting download of new URL file:///D:/x64Production.DLL.
    LOG: Assembly download was successful. Attempting setup of file: D:\x64Production.dll
    LOG: Entering run-from-source setup phase.
    LOG: Assembly Name is: x64Production, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
    ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

Has anyone else ascertained whether or not this is simply unsupported in the VS2010 mstest?

like image 262
David Avatar asked Feb 08 '12 23:02

David


4 Answers

I came here searching for the solution for a similar problem. Posting this answer just in case the solution I found helps someone else. This solved it for me in Visual Studio (2012):

Add New Item -> Test Settings Add Test Setting Item Change the testsetting Run test in 64 bit process By default this is set to "Force test to run in 32 bit process"

From the menu: Test -> Test Settings -> Select Test Settings File -> Choose the test settings file you have created.

Now run the tests.

like image 129
Anupam Avatar answered Nov 10 '22 18:11

Anupam


Now with Visual Studio 2013 (at least, didn't try in 2012) I didn't have to do anything but choose Test->Test Settings->Default Processor Architecture->x64. Can also use test settings file to achieve the same result. None of those old kluges necessary that you see in other answers and various posting on the web. Since my stuff has to use x64, I added these test cases too just to remind me if I have some setting wrong.

    [TestMethod]
    public void Running_64Bit_OS()
    {
        // It was determined to run only in 64 bits.
        bool is64BitOS = System.Environment.Is64BitOperatingSystem;
        Assert.AreEqual(is64BitOS, true);
    }

    [TestMethod]
    public void Running_64Bit_Process()
    {
        // We have a requirement that one of the unmanaged DLL is built for 64 bits.
        // If you are running MS Test in Visual Studio 2013 and this
        // is failing, go to Test->Test Settings->Default Processor Architecture and
        // chose x64, then run the tests again.  This is not the only method.  You
        // could also use a test settings file.
        bool is64BitProcess = System.Environment.Is64BitProcess;
        Assert.AreEqual(is64BitProcess, true);
    }
like image 32
JustTrying Avatar answered Nov 10 '22 18:11

JustTrying


Also, you can go to menu Test->Test Settings->Default Procesor Architecture->X64. It may work.

like image 19
user2545071 Avatar answered Nov 10 '22 18:11

user2545071


From reading this, MSTest.exe is 32 bit.

like image 16
Joshua Avatar answered Nov 10 '22 18:11

Joshua