I have a run time exception after changing Target Framework to .net framework 4: A first chance exception of type 'System.BadImageFormatException' occurred in
When building with target framework 3.5, everything works fine.
The platform i am building to is x86 (i found out that building to x64 night cause the problem).
What can be the problem?
Restart your web server and try again. Additionally, you can change the settings on Visual Studio. In my case, I went to Tools > Options > Projects and Solutions > Web Projects and checked Use the 64 bit version of IIS Express for web sites and projects - This was on VS Pro 2015. Nothing else fixed it but this.
This exception is thrown when the file format of a dynamic link library (. dll file) or an executable (.exe file) doesn't conform to the format that the common language runtime expects.
“An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to the 64-bit build of an application being deployed to IIS, which is being run in 32-bits.
MSDN lists possible reasons for this, so I'd suggest running through this as a checklist:
An attempt is made to load an unmanaged dynamic link library or executable (such as a Windows system DLL) as if it were a .NET Framework assembly.
A DLL or executable is loaded as a 64-bit assembly, but it contains 32-bit features or resources. For example, it relies on COM interop or calls methods in a 32-bit dynamic link library.
The idea is to make sure that all of your projects and dependencies are either compiled to target the same framework version, or a previous version; and that each of your projects are compatible in terms of bitiness; and if you're loading libraries dynamically, be sure to load them property (i.e. don't try to load native libraries as managed assemblies.)
Perhaps adding some more information about the configuration / dependencies of your project(s) would allow us to be more decisive.
Make sure all the projects in your solution are building to x86
or x64
or Any Cpu
- any mismatch can cause this problem.
Equally, if you are using any third party libraries - check out their target platform too.
Another thing to consider is whether this is being hosted in IIS - in which case you have to make sure that the bitness of your assemblies matches the bitness of the IIS hosting process. If you're on an x64 machine, then that's likely to be x64 (unless 32 bit hosting has been enabled).
I have to say I generally don't deviate from Any CPU
unless I have an external dependency on a COM component that's x86
or x64
only. It nearly always causes headaches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With