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 processRef: Rick Byers – exMSFT June 9, 2009
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.
- Open the Configuration Manager dialog box.
- In the Active solution configuration drop-down list, choose New.
The New Solution Configuration dialog box opens.
- In the Name text box, enter a name for the new configuration.
- To use the settings from an existing solution configuration, in the Copy settings from drop-down list, choose a configuration.
- 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 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.
- If you right click the required project and select properties, then the new window will open up.
- Under the Build tab on the left you can then see the ‘Platform target‘ selection.
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.