System.BadImageFormatException when target framework is 4.0
I have 开发者_Go百科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?
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.
- Components have been created using different versions of the .NET Framework. Typically, this exception occurs when an application or component that was developed using the .NET Framework 1.0 or the .NET Framework 1.1 attempts to load an assembly that was developed using the .NET Framework 2.0 SP1 or later, or when an application that was developed using the .NET Framework 2.0 SP1 or .NET Framework 3.5 attempts to load an assembly that was developed using the .NET Framework 4. The BadImageFormatException may be reported as a compile-time error, or the exception may be thrown at run time.
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.
Most likely, one of your assemblies references a x64 assembly when you are building for x86 or a x86 when you are building for x64.
I had this exact problem today, the BadImageFormatException
only happened when I changed the target framework to something > 3.5. All of the other answers on SO about this exception are talking about the bitness (32 vs 64) of our assemblies (which is a common problem, but doesn't apply to this case).
If the project runs fine when built against .NET 3.5 and throws BadImageFormatException on .NET 4.0 or greater, then be sure to check your App.config file for a supportedRuntime element. Mine said this:
<supportedRuntime version="v2.0.50727" sku="Client" />
This is going to force your program to run on the .NET 2.0 runtime, which can't load .NET 4.0 images (but it can load .NET 3.5 images). Any line that says this should be removed when switching to a framework version above 3.5.
If your project was running the first time and this error started to occur after changing your target framework, it can also mean that Visual Studio did some changes in the build and some of your libraries were not compatible any more.
So, Try going through the Build configurations.
Mostly this will help : Go to Application Properties > Build > Tick 'Prefer 32-bit'
If you are working on 4.5 framework, try unchecking the "Prefer 32-bit checkbox" option from "Debug" tab of project properties.
We were having the same problem with 4.5 framework. Tried many options. At last we unchecked "Prefer 32-bit checkbox" option from "Debug" tab of project properties and worked.
If you get this error when you start debugging from a dll project, make sure you point the executable and working directory to the expected executable and working directory.
you have to select debug mode instead of release mode in Solution Configurations.
In my case, it was because of a known issue in AppDynamics. Upgrading the version solved the issue.
Known issue: 4.5.x Resolved Issues by Month
Source: BadImageFormatException: Bad method token
I recently had this error for a different reason. Mine was related to Auto-generated binding redirects which is automatically enabled in .net 4.5.1 and later.
Microsoft docs: https://learn.microsoft.com/en-us/dotnet/framework/configure-apps/how-to-enable-and-disable-automatic-binding-redirection
"...you can easily disable autogenerated binding redirects in the project's property pages.
- Right-click the project in Solution Explorer and select Properties.
- On the Application page, uncheck the Auto-generate binding redirects option.
- Press Ctrl+S to save the change."
精彩评论