ChatGPT解决这个技术问题 Extra ChatGPT

Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format

c#

I just checked out a revision from Subversion to a new folder. Opened the solution and I get this when run:

Could not load file or assembly 'xxxx' or one of its dependencies. An attempt was made to load a program with an incorrect format.

This is the same code I had checked in a while ago. Why now is it doing this? I now also see a Debug x86 instead of just Debug in that xxx project's bin folder. What is Debug x86 and why don't I just have Debug only like I used to in the bin folder?


G
Grant Winney

Sounds like one part of the project is being built for x86-only while the rest is being built for any CPU/x64. This bit me, too. Are you running an x64 (or uh... IA64)?

Check the project properties and make sure everything is being built for "Any CPU". f you're in Visual Studio, you can check for everything by going to the "x86" or "Any CPU" menu (next to the "Debug"/"Release" menu) on the toolbar at the top of the screen and clicking "Configuration Manager..."


It's also at Project->Properties->Build, or Debug->Properties->Build. Just updated VS2015, version 14.0.25123.00 Update 2. This update was just released 5/10/16 (yesterday!). I found the Platform Target set to x64, which caused the error noted. Setting to "Any CPU" fixed it.
N
Nick Whiu

If you get this error while running the site in IIS 7+ on 64bit servers, you may have assemblies that are 32bit and your application pool will have the option "Enable 32-Bit Applications" set to False; Set this to true and restart the site to get it working.


@Mayhem50 Same here. It depends on where the error occurs. If it's through Visual Studio & Cassini web server, Fraser's right. If it occurs in IIS7+, Nicks answer is probably the most likely fix.
This was teh perfect answer for me (moved a site from IIS6 on x86 to IIS 7 on x64)
Yep, this did it. I moved a site from IIS6 on x86 to IIS 7 on x64. Thank you so much!
M
Mike Gledhill

I had this error when trying to use the dreadful Business Objects 4 for .Net SDK.

They ship five BusinessObjects*.dll files, but all of them are 64-bit.

To get my webpage to load, I needed to click on Tools\Options, then change this setting in VS2013:

https://i.stack.imgur.com/vpDnH.png


That option does not exist for me. The only option on this screen for me under Visual Studio 2010 is "Use IIS Express for new file-based web sites and projects"
I've this option in VS 2017 Professional
M
Muhammad Mubashir

inetmgr then come to Application pool->Advanced setting of your pool-> will have the option "Enable 32-Bit Applications" set to true; and restart IIS. check again.!


Second this. I created a new app pool for my site and had forgotten to toggle this setting.
This fixed it for me as well. The only other thing I needed to change was to change the Pipeline to integrated, as this gave me an error as well after the above fix was applied.
R
Rami Sarieddine

The BadImageFormatException on an application running on IIS (not running from VS, since visual studio fixes the problem by using the build for "Any CPU") can be caused by the following:

The site is one a server that is x64 and the Application Pool's default setting for Enable 32-Bit Applications was False. and you have 32-bit assemblies

On the level of Visual Studio, the fix is:

Change the project setting "Target CPU" to "ANYCPU"


D
Dan Gøran Lunde

Make sure you verify your setting for "Prefer 32-bit". In my case Visual Studio 2012 had this setting checked by default. Trying to use anything from an external DLL failed until I unchecked "Prefer 32-bit".

https://i.stack.imgur.com/UZYOZ.png


J
Jaco Pretorius

It's definitely an issue with some of the projects being built for x86 compatibility instead of any CPU. If I had to guess I would say that some of the references between your projects are probably referencing the dll's in some of the bin\debug folders instead of being project references.

When a project is compiled for x86 instead of 'Any CPU' the dll's go into the bin\x86\debug folder instead of bin\debug (which is probably where your references are looking).

But in any case, you should be using project references between your projects.


a
asdf_enel_hak

https://i.stack.imgur.com/sMliZ.png


R
Radu

In my case It worked by going to project Properties and under Target Framework i selected .NET Framework 4. This is because i have moved to a new machine that had other higher .NET frameworks already installed and the project selected them by default. See what target framework works for you.