System.BadImageFormatException when target framework is 4.0

北城余情 提交于 2020-01-10 12:12:10

问题


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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


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'




回答5:


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.




回答6:


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.




回答7:


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.




回答8:


you have to select debug mode instead of release mode in Solution Configurations.



来源:https://stackoverflow.com/questions/7319636/system-badimageformatexception-when-target-framework-is-4-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!