BadImageFormatException: How to Switch IIS Express to 64-Bit / 32-Bit for ASP.NET?

Are you getting the error below while running your ASP.NET Web Application?

Server Error in ‘/’ Application.
Could not load file or assembly ‘ClassLibrary1’ or one of its dependencies. An attempt was made to load a program with an incorrect format.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.BadImageFormatException: Could not load file or assembly ‘ClassLibrary1’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

From the message above, the .NET assembly ‘ClassLibrary1’ is the culprit. The ‘ClassLibrary1’ is an x64 bit library, but the ASP.NET application we want to run on IIS Express is 32-bit, which is also default bitness before Visual Studio 2022.

To change the bitness from x86 (32-bit) to x64 (64-bit), just go to Tools -> Options > Projects and Solutions -> Web Projects > Use the 64 bit version of IIS Express for web sites and projects and check it. This setting is available since VS 2013.

After that, run your project again and the error message should disappear.

Lastly, in the latest Visual Studio 2022, you can set the IIS Express bitness per project instead of all the projects you open in Visual Studio. Just right click your project on Solution Explorer > Properties > Web -> Bitness.

Leave a comment

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