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

34

533

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?

Lockyer answered 6/7, 2012 at 22:14 Comment(8)
If you have a version history in the repository, could you check if there are some differences in the csproj files?Icebox
@Steve: According to Mercurial, no changes other than adding references to new .cs filesLockyer
Do you get the same behaviour on another machine? Did anything else change on the machine (e.g. Windows update, dependency updates, etc.)?Vagus
Have you tried reverting those new .cs files?Vagus
This worked for me............ https://mcmap.net/q/74864/-could-not-load-file-or-assembly-exceptionBamboo
As a sanity check, in the Solution Explorer click Refresh. This let me save and rebuild the solution.Ascanius
Related post - Could not load file or assembly 'xxx' or one of its dependencies. An attempt was made to load a program with an incorrect format & Could not load file or assembly exceptionMalcommalcontent
Same problem. Looked everywhere for an answer. Had to step back and remove the problematic application then convert a previously backed up version as the original application. Thanks goodness I made it a habit to back up.Lowell
E
832

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.

The APP is set to run on 64-bit but the DLL is running on 32 bit to mitigate this navigate to IIS >> Application pool >> Advanced settings >> Enable 32 bit applications then set to true.

Emmons answered 7/7, 2012 at 1:47 Comment(9)
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!)Lockyer
Also verify that the project isn't Any CPU with Prefer 32 bit checked. Project -> properties -> buildDrin
PS: Another reason is "Enable 32-bit applications" being "false" in app pool settings. You need to restart IIS after setting it to true.Carsoncarstensz
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.Boren
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.Hazardous
Please set in IIS DefaultAppPool-> Enable 32-Bit Applications = TrueKure
If you have a Console as your front end, make sure the "Prefer 32-bit" flag is unchecked.Affect
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 hourEnsemble
It work for me, only uncheck prefer 32-bit and change platform target to x64. Thank youDisloyalty
Y
245

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

Yoheaveho answered 11/12, 2013 at 17:0 Comment(2)
Did I miss something? OP is talking about a console app not IIS deployment: "ProjectB is a console application, which depends on ProjectA"Cytochrome
In my case, it was set to true and worked after setting it to false. Same error message.Auditorium
K
192

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.

Konstance answered 6/6, 2016 at 20:9 Comment(5)
The opposite applies to, I had the 'Use 64 bit' ticked and needed to untick it...Lawrenson
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!Marcelmarcela
One word, Beauty!!Embattle
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.Decentralize
thanks, I was about to throw in the towel on this one. Happened when I went from VS 2019 to 2022.Beitnes
B
40

I had this same problem. I had set Project A's "Platform Target" ("Project A"(Right Click)->Properties->Build->"Platform Target") to x86 but kept Project B's at "Any CPU". Setting Project B's to "x86" fixed this.

Brunswick answered 18/4, 2013 at 16:23 Comment(0)
J
23

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
Judsonjudus answered 30/1, 2017 at 22:1 Comment(1)
Works like a charm for VS 2019 as well :)Pellerin
L
10

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.

Lambency answered 6/12, 2013 at 16:31 Comment(1)
OP is talking about a console app not IISCytochrome
I
10

First of all I got this in VS2017 with an old project I needed to make a tiny change to and upraded all the projects to framework 4.7.


Several others have mentioned selecting Any CPU can fix this issue.

There's a couple places you need to do it, and it might not just be as simple as selecting from the dropdown. This fixed it for me:

1) You need to do it both here:

enter image description here

2) And also in Configuration Manager (right click on solution)

enter image description here

But what if it isn't there???

Then click New and choose these settings: (thanks @RckLN)

enter image description here

Insure answered 2/3, 2018 at 6:18 Comment(0)
R
7

The following solved the issue for me, uncheck 'Prefer 32-bit' : enter image description here

Racoon answered 2/1, 2020 at 11:37 Comment(1)
Thanks, Have several solutions where the Prefer 32-bit is checked for the Net Framework project (4.72). Everything has been fine for a long time. But recently something changed. I think I tried by accident to recompile two of those solutions in VS2019 in stead of VS2022. Unchecking the Prefer 32-bit removes the BadImageFormatException in the two testprojects (NET7.0)Turnover
B
6

In my case changing IIS Express Bitness from "Default" to "x86" helped.

All my projects had "x86" as the Platform target.

VS 2022: Project Properties > Web

Birthright answered 7/12, 2022 at 15:22 Comment(0)
T
5

For the newer version of visual studio (v16.10 for this answer), it can be fixed by manually changing the solution platform. For me it worked after changing from "Any CPU" to "x86".

  1. Click on solutions platform dropdown, the one in which any CPU is appearing in image below. enter image description here

  2. Go to configuration manager.

    enter image description here

  3. Click on new and add platform x86 or x64 (32 or 64 bits) based on what is working for you. enter image description here

  4. Restart the project.

Tu answered 1/6, 2021 at 17:57 Comment(0)
M
4

I had the same issue with multiple projects in the same solution, i ended up setting all of the target frameworks to .NET Framework 4 and x86 for the target CPU and it finally successfully compiled.

Mishmash answered 7/8, 2013 at 1:24 Comment(1)
Worked in Release but failed in Debug. Set all to .Net Framework 4 (NOT Update 1) and Debug runs now.Cloven
S
4

None of these solutions worked for me - but by deleting the contents of bin and obj folders everything was cool again.

Swing answered 29/6, 2017 at 4:55 Comment(0)
N
4

I also had this problem. As mention before the problem was related to a 32-bit / 64-bit conflict, but with the site hosted in Azure. To change the plattform in Azure App Service, go to Configuration -> General settings. enter image description here

Nodule answered 9/11, 2021 at 10:22 Comment(0)
C
3

I got this when building a project via Visual Studio Online (VSTS) Build using Visual Studio Build Steps.

The solution was:

  • Delete the existing source folder
  • Explicitly set 'Any CPU' in the platform for all Visual Studio Builds including dependencies (see screenshot below).
  • Re-run the build

VSO Screenshot

Cy answered 15/9, 2017 at 10:50 Comment(0)
D
3

The Chilkat .NET 4.5 assembly requires the VC++ 2012 or 2013 runtime to be installed on any computer where your application runs. Most computers will already have it installed. Your development computer will have it because Visual Studio has been installed. However, if deploying to a computer where the required VC++ runtime is not available, the above error will occur:

Install all of the bellow packages

Visual C++ Redistributable Packages for Visual Studio 2013 - vcredist_x64

Visual C++ Redistributable Packages for Visual Studio 2013 - vcredist_x86

Visual C++ Redistributable Packages for Visual Studio 2012 - vcredist_x64

Visual C++ Redistributable Packages for Visual Studio 2012 - vcredist_x86

Disabled answered 11/12, 2017 at 10:43 Comment(0)
C
2

You might also see this issue if you're trying to package a 64bit project with an MSI installer in VS. ("The reason is because the native shim packaged with the .msi file is a 32-bit executable.")

See here for more details: http://blogs.msdn.com/b/heaths/archive/2006/02/01/64-bit-managed-custom-actions-with-visual-studio.aspx

Chlores answered 14/10, 2015 at 16:31 Comment(1)
Consider summarizing the linked article for benefit of future readers; in case the link goes dead.Charmain
S
2

In my project for C#, project property->[Build]->Platform target: Any CPU, and uncheck the Prefer 32-bit to let compiler to choose automatically.

Stripe answered 3/1, 2017 at 1:58 Comment(0)
O
1

I encountered the same issue. It popped up out of the blue and that seemed strange to me.

In the Exception snapshot, for the FusionLog, I saw the following within its message:

... C:\Windows\Microsoft.NET\Framework64 ...

More about the fusion log: http://msdn.microsoft.com/en-us/library/e74a18c4(v=vs.110).aspx

All the projects had a Target CPU of AnyCPU. I changed the application project (the project that references all the other projects) to a Target CPU of x86. It now works.

Not sure how the Target CPU mix up occurred for no apparent reason, but it did.

Ornate answered 8/11, 2013 at 15:41 Comment(0)
S
1

I also face this problem in a project, after a few minutes i found the solution, this problem is due to CPU configuration, If you are using Visual Studio 2010 or VS 2013, just goto project 's properties and then select Compile from side bar and there will be 5 drop-down, 5th Drop-down will be Target CPU:, you should set it to x86 or x64 according to your requirements instead of Any CPU.

My problem was solved after changing it to x86.

Selfregulating answered 11/9, 2015 at 10:12 Comment(0)
M
1

This also can happen just by having multiple supported frameworks defined in the app.config file and, forcing the app to run in a different .NET framework other than the one mentioned first in the app.config file.

And also this fires when you have both of the mentioned frameworks available in your system.

As a workaround, bring up the target framework you are going to use for the debugging up in the app.config

ex: if you trying to run in .NET 4, config file should have something similar to this,

<supportedRuntime version="v4.0"/>
<supportedRuntime version="v2.0.50727"/>
Mandymandych answered 30/8, 2016 at 10:18 Comment(0)
T
1

I also had this problem running unit tests by using ReSharper on Visual Studio 2017 and fixed it with following config:

enter image description here

Also you can change the ReSharper's run test setting: https://resharper-support.jetbrains.com/hc/en-us/articles/207242715-How-to-run-MSTest-tests-using-x64-configuration

Tonneau answered 31/1, 2018 at 5:32 Comment(0)
G
1

Shoot! I knew about this problem. I thought I was doing everything right until I accidentally saw 'x86' in the VS output window and that's when I got hold of the cause. Wasted a few mins on it today.

The configuration under 'Publish' window was set to 'x86'; whereas, everywhere else, it was 'x64'.

Please make sure it's in-sync across configuration manager, publish settings, solution configurations, and IIS settings (if that's your web server).

Also, please keep in mind - VS is a 32-bit app and IIS is 64 bit. 32-bit apps are disabled by default in IIS.

enter image description here

Gefen answered 18/3, 2019 at 3:37 Comment(0)
F
1

The configuration manager showed all projects set to Configuration - Debug, Platform x86.

enter image description here

The project file for the offending project had PlatformTarget x64 for Configuration|Platform Debug|x86 though. Changing the PlatformTarget in the project file fixed the problem.

enter image description here

Fascinate answered 24/10, 2023 at 13:10 Comment(0)
A
0

It can be a little funny, but I had the same problem with normal working code. I added StreamWriter and StreamReader and it gave that error. The solution was I took that code into comment brackets then did debug and it started to work again

Apologia answered 21/10, 2015 at 18:55 Comment(0)
B
0

If you use LibreOffice from your program via cli .net integration like me, I got the same error. I use the older version of LibreOffice on the production environment on my PC I installed a newer version that was in conflict. Just uninstall LibreOffice. I found the solution here .NET CLI: Could not load file or assembly 'cli_cppuhelper'

Brighten answered 17/4, 2018 at 8:55 Comment(0)
D
0

In my case a dependency was missing in the dll that threw this exception. I checked with Dependency Walker, added the missing dll and the problem was resolved.

More specifically, I somehow corrupted my opencv_core340.dll by accidentally adding SVN keywords to it, and thus my dll could no longer use it. However I don't believe that the solution to this problem depends on whether the dll is corrupted or missing. I'm just adding this for the sake of giving complete information.

Dishonest answered 31/10, 2018 at 16:22 Comment(0)
E
0

I have detected something different from the other answers. Reaching this exception in my project was the result of a corrupt compilation. Without making any changes, just forcing rebuild, it was fixed.

Essary answered 30/1, 2020 at 8:27 Comment(0)
L
0

I had the same issue. Project B in my case was a .Net Core Class Library which has a Nuget "Microsoft.Management.Infrastructure" installed. The error was that i called my project B "MI". I changed the project name to something else and suddenly everything worked again.

Loiret answered 24/3, 2020 at 10:8 Comment(0)
S
0

Interesting as it goes, this can also happen if the folder path is long, which can cause build issues, oddly enough with this cryptic error message.

Just moving the folder up the path, solved the problem!

Store answered 8/4, 2022 at 0:15 Comment(0)
I
0

Another reason for this (within github actions or azure pipelines) is that you have large files in your git repo and have not enabled git lfs within your checkout step:

 - name: Checkout code
      uses: actions/checkout@v2
      with:
        lfs: true
Interrex answered 8/3 at 18:39 Comment(0)
D
0

If you are facing this issue on MACOS it might be related to the project being set up to specifically work on x64 architecture.

Try to install the x64 version of the SDK your project is using and run the project using the dotnet run -a x64 command.

This solved my issue, so hopefully this can help someone else.

Disburden answered 10/4 at 10:23 Comment(0)
C
-1

Are you trying to run your .exe file from the cmd? This was my mistake. Just run the .exe file by double clicking it. If it's a .NET Core SCD for Windows 8.1/Windows Server 2012 R2 x64.

Canarese answered 27/3, 2018 at 7:29 Comment(0)
E
-1

In my case the error was System.BadImageFormatException: Could not load file or assembly 'vjslib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies.

It was solved by installing vjredist 64 from here.

Enforce answered 28/7, 2020 at 3:21 Comment(0)
B
-2

My machine showed me a BIOS update and I wondered if that has something to do with the sudden popping-up of this error. And after I did the update, the error was resolved and the solution built fine.

Boutique answered 31/1, 2017 at 19:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.