BadImageFormatException during .Net assembly load issue

19

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
c#
asp.net
.net
visual-studio-2010
iis
asked on Stack Overflow Aug 29, 2010 by George2 • edited Dec 29, 2016 by jessehouwing

4 Answers

16

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.

answered on Stack Overflow Aug 29, 2010 by Abel • edited Aug 29, 2010 by Abel
8

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.

answered on Stack Overflow Sep 7, 2015 by GHC
2

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

answered on Stack Overflow Aug 29, 2010 by sagie
0

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
answered on Stack Overflow Jun 28, 2019 by Jon

User contributions licensed under CC BY-SA 3.0