23

When I try to access a page (default.aspx) in a web site in IIS 7.0 (developed using VSTS 2010 + .Net 4.0 on Windows Server 2008), I met with the following error message. Any ideas what is wrong? What means BadImageFormatException?

BTW: I am running Windows Server 2008 64 bit (not R2) and not activated Windows Server 2008 yet, could it be a root cause?

[BadImageFormatException: Could not load file or assembly 'test.foo' or one of its dependencies. Try to load bad formatted program. ]
   System.Reflection.RuntimeAssembly._nLoad(AssemblyName fileName, String codeBase, Evidence assemblySecurity, RuntimeAssembly locationHint, StackCrawlMark& stackMark, Boolean throwOnFileNotFound, Boolean forIntrospection, Boolean suppressSecurityChecks) +0
   System.Reflection.RuntimeAssembly.InternalLoadAssemblyName(AssemblyName assemblyRef, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection, Boolean suppressSecurityChecks) +567
   System.Reflection.RuntimeAssembly.InternalLoad(String assemblyString, Evidence assemblySecurity, StackCrawlMark& stackMark, Boolean forIntrospection) +192
   System.Reflection.Assembly.Load(String assemblyString) +35
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +118

[ConfigurationErrorsException: Could not load file or assembly 'test.foo' or one of its dependencies.  Try to load bad formatted program. ]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +11392147
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +484
   System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +127
   System.Web.Compilation.BuildManager.GetReferencedAssemblies(CompilationSection compConfig) +334
   System.Web.Compilation.BuildManager.CallPreStartInitMethods() +280
   System.Web.Hosting.HostingEnvironment.Initialize(ApplicationManager appManager, IApplicationHost appHost, IConfigMapPathFactory configMapPathFactory, HostingEnvironmentParameters hostingParameters, PolicyLevel policyLevel, Exception appDomainCreationException) +1087

[HttpException (0x80004005): Could not load file or assembly 'test.foo' or one of its dependencies.  Try to load bad formatted program. ]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11524352
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4782309
jessehouwing
  • 106,458
  • 22
  • 256
  • 341
George2
  • 44,761
  • 110
  • 317
  • 455
  • 1
    I think your assembly name lacks ".dll". Please provide the code snippet. – Andreas Rehm Aug 29 '10 at 14:33
  • 3
    64-bit/32 bit mismatch? Did you compile your assemblies for a specific platform? – Alex Aug 29 '10 at 14:38
  • "I think your assembly name lacks ".dll". Please provide the code snippet." -- From the stack track, I think all code stack trace are not in my own code, it seems .Net own code? What .dll do I need to provide and what code snippet do you need in order to analyze this issue? – George2 Aug 29 '10 at 14:42
  • "64-bit/32 bit mismatch? Did you compile your assemblies for a specific platform?" -- How to check? And what is your preferred build option (e.g. build or 32 bit or 64 bit)? Specific platform you mean? – George2 Aug 29 '10 at 14:42
  • 1
    @George2: it is indeed not in your code, the assembly loader raises this exception (you can have your own assembly loader, but that doesn't seem the case here). About the 32/64 mismatch: specify 32 bits when running on 64 bits (for *why* see my answer). – Abel Aug 29 '10 at 14:50
  • 1
    @George2 - usually you shouldn't specify the platform type. You should use "Any CPU", and let the host\framework choose the platform in runtime. The current selected platform type is displayed in a box in the VS menu bar. If it is not displayed, you can add it from tools->Customize->commands->Add command->Build->Solution Platforms – sagie Aug 29 '10 at 14:52
  • 1
    PS: it is probably a good thing to upgrade to R2. Microsoft has fixed a gigantic list of issues, not a few of them related to .NET. So, if the mentioned fixes don't help or don't apply, make sure to upgrade everything to its latest versions (!). – Abel Aug 29 '10 at 14:55
  • The lastest Windows Server version is Windows Server 2008 R2? – George2 Aug 29 '10 at 15:24
  • Thanks for your advice, sagie and Abel! I will try tomorrow when I am in office and let you know the results. :-) – George2 Aug 29 '10 at 15:25
  • Platform isn't only x64/x86/AnyCpu. I've encountered this exception while checking the assembly version of a bunch of FSharp.Core.dlls. Several ngen/native images and mono builds where among the obstacles. – mbx Jun 07 '17 at 08:03

4 Answers4

20

The BadImageFormatException is raised when the assembly file can be found, but is not a proper assembly, or is corrupted. For instance, if you FTP'ed the files to the server and the transfer was interrupted, the DLL file may have been transferred only partially, causing this error to happen.

On 64 bit vs 32 bit: when you use P/Invoke or COM Interop, some blogger reports that switching to a specific target compilation may help your situation. This means: if you interface with a 32 bit dll, make sure that you compile for x86, forcing it to run under WoW32, otherwise you'll receive this exception. This fix is confirmed here and here.

Alternatively, you can set your whole system to default 32-bit by running the command:

Ldr64.exe setwow

from the Framework64 directory.

A common solution is to rebuild the file, or to at least re-publish it.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • You mean test.foo.dll itself is corrupted or some other system DLLs? – George2 Aug 29 '10 at 14:44
  • 1
    @George2: I mean test.foo.dll is corrupted, but see my new notes on the 32 bit vs 64 bit, which may seem counter-intuitive. – Abel Aug 29 '10 at 14:47
  • Thanks for your update @Abel, do you mean I should rebuild the solution to 64-bit platform (since my Windows Server 2008 is 64-bit)? – George2 Aug 29 '10 at 14:48
  • Could this issue be caused by my Windows Server 2008 not activated (I did not entered a license number yet)? – George2 Aug 29 '10 at 14:50
  • 1
    @George2: that depends. If you do not use COM Interop or P/Invoke to 32 bit COM or native DLLs, you can stick to Any and the issue is somewhere else. If you do use P/Invoke or COM Interop: *change to 32 bit* and test if that works. Note: when running on 64 bit, the .NET boots trapper will select 64 bit and compiles as such, but if you *need* 32 bit, you must compile specifically to that platform (even if you *run* on 64 bit). – Abel Aug 29 '10 at 14:52
  • I did not use COM Interop or P/Invoke in my code, my application is just simple web application. I think I should use build for Any Platform, correct? – George2 Aug 29 '10 at 15:04
  • 1
    @George2: correct. (I missed your q. about non-activation: don't worry, Windows is fully functional, it will not block your self-made code and definitely not with a BadImageFormatException...). – Abel Aug 29 '10 at 16:47
12

I just got this when deploying 32 bit DLLs onto a 64 bit server running IIS 7.

To fix it, I had to set "Enable 32-Bit Applications" to True in the Advanced Settings of my application pool.

GHC
  • 2,658
  • 5
  • 30
  • 35
  • 1
    THANK YOU! This probably saved me several hours of trying to recompile a legacy app for 64-bit. – laindir May 07 '21 at 21:18
3

Does your web site uses the DefaultAppPool? If so, try setting the application pool of your web site to ASP .Net v4.0, Or if you are using a customized app pool, verify that it is running .net framework 4.0

sagie
  • 2,998
  • 3
  • 22
  • 31
  • I am using a new application pool. I set it to use .Net 4.0 Framework already and using integrated mode. Any ideas? – George2 Aug 29 '10 at 14:43
  • Could this issue be caused by my Windows Server 2008 not activated (I did not entered a license number yet)? – George2 Aug 29 '10 at 14:50
  • 1
    With which platform you are building your solution? Any CPU? x86? I don't think it is an activation issue. Are you using any third parties? – sagie Aug 29 '10 at 14:54
  • I am not using 3rd parties. If I refer to another DLL I developed by myself, is it 3rd party? – George2 Aug 29 '10 at 15:26
  • And with which platform this Dll was built with? – sagie Aug 30 '10 at 11:04
1

This happened to me in a VM where I needed to enable 32 bit applications in the App-Pool and was able to do that using powershell:

Import-Module WebAdministration
Set-ItemProperty IIS:\AppPools\DefaultAppPool\ -Name enable32BitAppOnWin64 -Value $true
Jon
  • 9,156
  • 9
  • 56
  • 73