16

There are a lot of questions on this forum about the BadImageFormatException, but none quite matches my issue.

I have a solution containing several projects. When I run in Debug or Release + Any CPU, everything works fine. When I switch to x64, however, and run my ASP.NET MVC project, I get the infamous BadImageFormatException

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

AgileEFLib is an in-house utility library, so I have plenty flexibility with that. But the really weird thing is that when I run a console app that also has a dependency on AgileEFLib, it works perfectly. The only time it fails is when I run the web site.

I have checked that all projects in the Release|x64 configuration are set to compile as x64.

What else can I do to troubleshoot this?

EDIT: Another useful bit of information is that this problem only occurs when I'm debugging using Visual Studio's IIS Express. When I publish and deploy to IIS, it works fine.

EDIT: more details about the error:

Server Error in '/' Application.
--------------------------------------------------------------------------------

Could not load file or assembly 'AgileEFLib' or one of its dependencies. An attempt was made to load a program with an incorrect format. 
  Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

 Exception Details: System.BadImageFormatException: Could not load file or assembly 'AgileEFLib' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Source Error: 


 An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Assembly Load Trace: The following information can be helpful to determine why the assembly 'AgileEFLib' could not be loaded.



=== Pre-bind state information ===
LOG: DisplayName = AgileEFLib
 (Partial)
WRN: Partial binding information was supplied for an assembly:
WRN: Assembly Name: AgileEFLib | Domain ID: 3
WRN: A partial bind occurs when only part of the assembly display name is provided.
WRN: This might result in the binder loading an incorrect assembly.
WRN: It is recommended to provide a fully specified textual identity for the assembly,
WRN: that consists of the simple name, version, culture, and public key token.
WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
LOG: Appbase = file:///C:/src/MyProject/
LOG: Initial PrivatePath = C:\src\MyProject\bin
Calling assembly : (Unknown).
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\src\MyProject\web.config
LOG: Using host configuration file: C:\Users\Shaul\Documents\IISExpress\config\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/15406131/a0c36da/AgileEFLib.DLL.
LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/15406131/a0c36da/AgileEFLib/AgileEFLib.DLL.
LOG: Attempting download of new URL file:///C:/src/MyProject/bin/AgileEFLib.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
Shaul Behr
  • 36,951
  • 69
  • 249
  • 387
  • What version of IIS Express? Also, did you enable `enable32BitAppOnWin64` flag? – haim770 May 26 '14 at 09:17
  • @haim770, this happens in both VS 2012 and VS 2013, not sure what version of IIS Express that makes it. I'm interested in that `enable32BitAppOnWin64`, because I've never heard of it before. Where would I find it? – Shaul Behr May 26 '14 at 09:23
  • Check the `Server` response header for the version. `enable32BitAppOnWin64` is a configuration flag on the application pool settings. – haim770 May 26 '14 at 09:26
  • @haim770: Server: Microsoft-IIS/8.0 – Shaul Behr May 26 '14 at 09:28
  • If you go through your bin folder, are all the dependencies (http://stackoverflow.com/questions/270531/how-to-determine-if-a-net-assembly-was-built-for-x86-or-x64/) either x64 or Any CPU ? – Ondrej Svejdar May 26 '14 at 09:59
  • I had this same problem when I was running in an IIS app pool with "Enable 32-bit Applications" set to TRUE. When I changed it to FALSE, the BadImageFormatException went away. – Debbie A Aug 05 '15 at 16:36
  • For Visual Studio 2019, see the [procedure below](https://stackoverflow.com/a/60159618/645511). – Katie Kilian Feb 10 '20 at 22:50

4 Answers4

23

Try to follow the instructions from Debugging VS2013 Websites Using 64-bit IIS Express:

If you are working on ASP.NET MVC web sites in Visual Studio 2013 (VS2013), you need to make one registry change if you want to run IIS Express as a 64-bit process by default. Use one of the methods, below.

Command-Line:

reg add HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\12.0\WebProjects /v Use64BitIISExpress /t REG_DWORD /d 1

haim770
  • 48,394
  • 7
  • 105
  • 133
2

In my case, for Visual Studio 2019, there was no need to edit the registry manually. I followed the instructions found here.

When you launch a web project in Visual Studio, by default it uses the 32-bit version of IIS Express. To change that you can enable the 64-bit version through Options.

So, head to:

Tools --> Options --> Project and Solutions --> Web Projects

Check the option “Use the 64 bit version of IIS Express for web sites and projects”

This fixed it for me.

Katie Kilian
  • 6,815
  • 5
  • 41
  • 64
1

You can use Fuselogvw.exe. This utility dumps all Fusion activity to a log. You can then go through the log and check which DLL is of the wrong format. You have to also see what AgileEFLib actually depends on. Maybe the DLL itself is not the problem but one of its dependencies.

I guess that one of the dependency DLLs in your website is compiled as an x86.

Edit: Also see Dependency Walker. This one shows all dependencies and their architecture.

Farhad Alizadeh Noori
  • 2,276
  • 17
  • 22
1

Rebuild the referenced assembly using the "AnyCPU" setting. This would allow the class contained in it to be instantiated within a 32-bit process, or in a 64-bit process.

wakthar
  • 720
  • 1
  • 8
  • 21
  • Doesn't work, but it helps narrow down the problem: I made AgileEFLib use "Any CPU", and the problem shifted to another dependency. I progressively changed them all to use "Any CPU", until finally the web project itself refused to run unless I switched it to "Any CPU". That kinda defeats the purpose of using x64, doesn't it? – Shaul Behr May 21 '14 at 15:11
  • 1
    Somewhere down the line you may have a 32bit dll being used. An AnyCPU assembly will JIT to 64 bit code when loaded into 64 bit process and 32 bit when loaded into a 32 bit process. By limiting the CPU you would be saying there is something being used by the assembly (something likely unmanaged) that requires 32 bits or 64 bits. – wakthar May 22 '14 at 09:35
  • Seriously? You mean I've been driving myself crazy for nothing; I can just compile everything with "Any CPU" and it'll run as a 64-bit program on a 64-bit machine? – Shaul Behr May 22 '14 at 15:56
  • Update: tested your theory. Built my project using "Any CPU". Ran out of memory at ~3GB. – Shaul Behr May 22 '14 at 17:51
  • Publish to IIS, create a new App Pool for your application and in advanced settings for app pool, Enable 32-bit Applications. – wakthar May 23 '14 at 08:53
  • I'm trying to run in debug mode, under the .NET IIS Express. – Shaul Behr May 25 '14 at 14:51
  • 1
    @Shaul Yes, Any CPU will run as 64-bit on 64-bit machine (just make sure you don't have "prefer 32-bit" checked in build options) - but everything in the pipeline has to be 64-bit. Verify that `w3wp.exe` (or `IISExpress.exe` for IIS express) is actually running as a 64-bit application - by default, it doesn't. When the host process is running in 32-bit, so will your DLL's code :) – Luaan May 26 '14 at 09:59
  • In my case the solution is to set `Platform target = Any CPU` in the properties of the project. – Tonatio Jul 12 '16 at 17:17