Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException)
Solution 1
I am pretty sure you're having a 32-bit / 64-bit conflict. It sounds like your main project might be set to 32-bit while the class its referencing is set to 64-bit. Try looking at this SO question and this one too. Between the two of them, you should be able to figure out your problem.
Solution 2
Might be you are facing the problem with your website after deploying on server.
Then you need to adjust your application pool to Enable 32-Bit Applications.
Steps
- Open IIS Manager
- Click on Application Pools
- Select whatever application pool you are using
From right pane, click Advanced Settings...
-
Set Enable 32-Bit Applications to True
Solution 3
I just had this error message running IIS Express in Visual Studio 2015. In my case I needed to be running the 64 bit version of IIS Express:
Tools → Options → Projects and Solutions → Web Projects
Check the box that says "Use the 64 bit version of IIS Express for web sites and projects".
Screenshot:
Solution 4
I had this problem running unit tests (xunit) in Visual Studio 2015 and came across the following fix:
Menu Bar -> Test -> Test Settings -> Default Processor Architecture -> X64
Solution 5
You may need to change the Appication Pool setting "Enable 32bit Applications" to TRUE in IIS7 if you have at least 1 32bit dll\exe in your project.
![BlueRaja - Danny Pflughoeft](https://i.stack.imgur.com/SINHF.png?s=256&g=1)
BlueRaja - Danny Pflughoeft
Ringotan - Learn Japanese Kanji A blog, sort of
Updated on November 10, 2021Comments
-
BlueRaja - Danny Pflughoeft over 2 years
I have two projects,
ProjectA
andProjectB
.ProjectB
is a console application, which depends onProjectA
. Yesterday, everything was working fine, but suddenly today when I runProjectB
I get this:BadImageFormatException was unhandled:
Could not load file or assembly 'ProjectA, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.Both are just regular projects, with no dependencies on any other non-.Net projects. Both are fully .Net - there is no native code, and no P/Invoke. I have other projects which depend on
ProjectA
and still work just fine.Things I have tried:
- Make sure both projects are set to "Any CPU," with the build checkbox checked. They are.
- Make sure both projects are for the same Target Framework (.Net 4.0 Client Profile).
- Under ProjectB --> References --> ProjectA --> Properties, make sure "Copy Local" is set to "True" _ (I verified that ProjectA.dll is being copied correctly)
- Clean/Rebuild the solution. I even tried manually deleting the /bin and /obj folders in both projects.
- Restart Visual Studio. Restart my computer.
- Check out an entirely new copy of the repository.
But I still get the same error. I have no idea what I did to cause this, nor how to fix it. Any ideas?
-
BlueRaja - Danny Pflughoeft almost 12 yearsDo'h. I somehow completely missing "platform target" dropdown in
project-->properties-->build
- it was set for x86; setting it to "Any CPU" fixed this issue. I always thought that this setting was the same as the "platform target" dropdown in the configuration manager, but apparently it's not (in fact, the "platform target" in configuration manager doesn't appear to do anything at all!) -
Reid Evans over 10 yearsAlso verify that the project isn't Any CPU with Prefer 32 bit checked. Project -> properties -> build
-
dvdmn about 10 yearsPS: Another reason is "Enable 32-bit applications" being "false" in app pool settings. You need to restart IIS after setting it to true.
-
jahu almost 9 yearsThe worst that happened to me with this error was when VS decided to append
<PlatformTarget>x86</PlatformTarget>
in one of the dependent projects for no reason at all. If I haven't looked into SVN I would have never figured out why our MVC app fails to launch. -
Bond - Java Bond over 8 yearsConsider summarizing the linked article for benefit of future readers; in case the link goes dead.
-
DCastenholz over 8 yearsWorked in Release but failed in Debug. Set all to .Net Framework 4 (NOT Update 1) and Debug runs now.
-
MickyD over 8 yearsDid I miss something? OP is talking about a console app not IIS deployment: "ProjectB is a console application, which depends on ProjectA"
-
MickyD over 8 yearsOP is talking about a console app not IIS
-
Michael D. O'Connor about 8 yearsJust 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.
-
cjb110 over 7 yearsThe opposite applies to, I had the 'Use 64 bit' ticked and needed to untick it...
-
Shantu over 6 yearsPlease set in IIS DefaultAppPool-> Enable 32-Bit Applications = True
-
Stamatis Tiniakos almost 4 yearsGreat answer @AliAdravi
-
John over 3 yearsYup, I switched my web app to 64 bit to ensure I had the memory overhead, and then I needed to tick this option to get it to load after that. Thanks!
-
el_nektarin over 3 yearsWorks like a charm for VS 2019 as well :)
-
Bertie about 3 yearsIf you have a Console as your front end, make sure the "Prefer 32-bit" flag is unchecked.
-
Jamshaid K. almost 3 yearsOne word, Beauty!!
-
planetregin almost 3 yearsIn my case, it was set to true and worked after setting it to false. Same error message.
-
mikus over 2 yearsThat was it for me, it's easy to forget about this setting, once you're jumping into a fresh environment, it really feels like it should be either on by default or bound to the project target platform. Also easy to forget that if you're not using full IIS there is actually something to configure out there.
-
Olaoluwa over 2 yearsFor me, i think while I was making modifications to my XML documentation target on that project ->build settings i must have scrolled the platform target to AMD64. Cost me another hour
-
Anton Nikolayevich over 2 yearsIt work for me, only uncheck prefer 32-bit and change platform target to x64. Thank you