BadImageFormatException for .NET Web Applications

During development in Microsoft Visual Studio, Web applications are often executed with the integrated Web server “IIS Express”, which is started as a 32-bit process by default.

The projects are created in Visual Studio as “Any CPU”, which means that the Web application supports both x86 and x64 platforms at runtime. When running the Web Application, an exception ‘System.BadImageFormatException’ may occur if the List & Label .NET components have been added as reference (e.g. via NuGet):

Could not load file or assembly ‘combit.ListLabel[??]’ or one of its dependencies. An attempt was made to load a program with an incorrect format.

Typical example:

Background, explanation and solution
List & Label of course provides the .NET components as Any CPU. But there is a special circumstance: the .NET components of List & Label require native modules from List & Label, which are available as x86 and x64 DLLs. If Any CPU is selected, the unmanaged modules are then also copied to the output folder of the application as both x86 and x64. An overview of the modules can be found in the file Redist.txt. Details on redistribution can be found in the .NET help under Deployment.

However, the IIS(-Express) performs a so-called preloading of all modules to optimize performance, inevitably encountering a mixture of x86 and x64 modules and acknowledging this with the above error message.

For Web applications, the following therefore applies: In order for the application to be executed, the bitness of the application in Visual Studio or the IIS Express used must match. This can either be configured generally for the Visual Studio installation or specifically for the Web project:

Global for Visual Studio
In the options under Tools > Options > Projects and Solutions > Web Projects, you can set which platform the IIS Express should run on:

In the properties of the project under Project > Properties > Web > Servers, you can set the platform on which the IIS Express is to run:

Note: Please also consider this information when the Web Application is finally published, as the IIS process or the application pool used is also executed on a specific platform - x86 or x64.

Exception at start of a .NET application

© combit GmbH