Could not load file or assembly ... An attempt was made to load a program with an incorrect format (System.BadImageFormatException)

559,568

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

  1. Open IIS Manager
  2. Click on Application Pools
  3. Select whatever application pool you are using
  4. From right pane, click Advanced Settings...

  5. Set Enable 32-Bit Applications to True

    Advanced Settings Enable 32-Bit

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:

Screenshot of VS options for Web Project.

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.

Share:
559,568
BlueRaja - Danny Pflughoeft
Author by

BlueRaja - Danny Pflughoeft

Ringotan - Learn Japanese Kanji A blog, sort of

Updated on November 10, 2021

Comments

  • BlueRaja - Danny Pflughoeft
    BlueRaja - Danny Pflughoeft over 2 years

    I have two projects, ProjectA and ProjectB. ProjectB is a console application, which depends on ProjectA. Yesterday, everything was working fine, but suddenly today when I run ProjectB 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
    BlueRaja - Danny Pflughoeft almost 12 years
    Do'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
    Reid Evans over 10 years
    Also verify that the project isn't Any CPU with Prefer 32 bit checked. Project -> properties -> build
  • dvdmn
    dvdmn about 10 years
    PS: Another reason is "Enable 32-bit applications" being "false" in app pool settings. You need to restart IIS after setting it to true.
  • jahu
    jahu almost 9 years
    The 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
    Bond - Java Bond over 8 years
    Consider summarizing the linked article for benefit of future readers; in case the link goes dead.
  • DCastenholz
    DCastenholz over 8 years
    Worked in Release but failed in Debug. Set all to .Net Framework 4 (NOT Update 1) and Debug runs now.
  • MickyD
    MickyD over 8 years
    Did I miss something? OP is talking about a console app not IIS deployment: "ProjectB is a console application, which depends on ProjectA"
  • MickyD
    MickyD over 8 years
    OP is talking about a console app not IIS
  • Michael D. O'Connor
    Michael D. O'Connor about 8 years
    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.
  • cjb110
    cjb110 over 7 years
    The opposite applies to, I had the 'Use 64 bit' ticked and needed to untick it...
  • Shantu
    Shantu over 6 years
    Please set in IIS DefaultAppPool-> Enable 32-Bit Applications = True
  • Stamatis Tiniakos
    Stamatis Tiniakos almost 4 years
    Great answer @AliAdravi
  • John
    John over 3 years
    Yup, 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
    el_nektarin over 3 years
    Works like a charm for VS 2019 as well :)
  • Bertie
    Bertie about 3 years
    If you have a Console as your front end, make sure the "Prefer 32-bit" flag is unchecked.
  • Jamshaid K.
    Jamshaid K. almost 3 years
    One word, Beauty!!
  • planetregin
    planetregin almost 3 years
    In my case, it was set to true and worked after setting it to false. Same error message.
  • mikus
    mikus over 2 years
    That 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
    Olaoluwa over 2 years
    For 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
    Anton Nikolayevich over 2 years
    It work for me, only uncheck prefer 32-bit and change platform target to x64. Thank you