BadImageFormatException When Running 32/64 Bit Applications in Visual Studio

You might be about to run your application and suddenly you are getting ‘System.BadImageFormatException’. This is what I got when running a new application that I didn’t build. It took me a little bit to figure out what the issue was, but as below, the culprit was found and also some other interesting configurations required.

The problem I found was the application was built using a DotNet Core Console Application, but specifically using the x64 Bit processor. Now this is not a problem of course, as you want it to use the best performance processor the application can, so you can deliver that performance to your end users. However, it seems the rest of Visual Studio was not ready for this, so running Local IIS or running Unit Tests was causing a System.BadImageFormatException exception.

When I have been building application before, I have just defaulted to using the ‘AnyCpu’ configuration, which has worked perfect with no conflict or issues. What this actually does under the hood though, is it chooses the lowest configuration it can depending on the requirements of the application, which for this case was 32bit.

When an application can run fine either in 32-bit or 64-bit mode, the 32-bit mode tends to be a little faster. Larger pointers means more memory and cache consumption, and the number of bytes of CPU cache available is the same for both 32-bit and 64-bit processes. Of course the WOW layer does add some overhead, but the performance numbers I’ve seen indicate that in most real-world scenarios running in the WOW is faster than running as a native 64-bit process

Ref: Rick Byers – exMSFT June 9, 2009

Fixing This

If you still want to run specifically as 64 bit or 32 bit then there are a few places this needs to be changed for both IIS Express and Unit Tests to work.

Build Configuration

  1. Open the Configuration Manager dialog box.

    undefined
  2. In the Active solution configuration drop-down list, choose New.
    The New Solution Configuration dialog box opens.
     
    undefined
  3. In the Name text box, enter a name for the new configuration.
  4. To use the settings from an existing solution configuration, in the Copy settings from drop-down list, choose a configuration.
     
    undefined
  5. If you want to create project configurations at the same time, select the Create new project configurations check box

You can get further instructions from Microsoft Official Documentation for other setups and starting points.

IIS Express

IIS Express is what is used when running the application from Visual Studio. Each project has it’s own setting for this, so if you have multiple projects, then you will need to do the following for each of them.

  1. If you right click the required project and select properties, then the new window will open up.
  2. Under the Build tab on the left you can then see the ‘Platform target‘ selection.

Unit Tests

Yes even the tests can run under a different process, so we need to configure them as well. This is global to all test projects, so will onlt need to be done once. You can configure them seperately, but doing the about section for each project.

In the ‘Test‘ option at the top of the window select the ‘Processor Architecture for AnyCPU Projects‘ then the desired processor setting.

undefined

Published by Chris Pateman - PR Coder

A Digital Technical Lead, constantly learning and sharing the knowledge journey.

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: