Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BadImageFormatException during .Net assembly load issue

Tags:

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 
like image 554
George2 Avatar asked Aug 29 '10 14:08

George2


People also ask

How to fix system BadImageFormatException?

SOLUTION: If you are on IIS7, Right Click/ Advanced Settings on the Specific Application Pool of the website and select True on Enable 32-Bit Applications. It should work.

What is System BadImageFormatException?

Solution. A System. BadImageFormatException error often occurs when a 64-bit assembly is opened by a 32-bit application. In this case, Visual Studio is a 32-bit application and the Design view is attempting to load a 64-bit assembly. Visual Studio assemblies are located in the project references tree.

Could not load the file or assembly or one of its dependencies?

In summary if you get the "Could not load file or assembly error", this means that either your projects or their references were built with a reference to a specific version of an assembly which is missing from your bin directory or GAC.

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

“An attempt was made to load a program with an incorrect format.” That means that the assembly, which was to be loaded, was in an unexpected format. The format, in this case, refers most likely to the 64-bit build of an application being deployed to IIS, which is being run in 32-bits.


1 Answers

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.

like image 169
Abel Avatar answered Oct 03 '22 16:10

Abel