IIS 7.5 Fixing An attempt was made to load a program with an incorrect format problem?

I have a unusual problem throwing an exception "An attempt was made to load a program with an incorrect format" error? I have two identical websites on the same IIS server and my build configuration works for one of them but not the other.

My C# MVC 2 web app can deployed to two websites that reside on the same IIS 7.5 webserver (x64). One is the live site (deployed using Release configuration), the second is the beta site (deployed using a new Beta configuration created just for this project). The codebase for both projects is the same. They have different build configurations, but the settings inside the configurations are identical.

Deploying the release configuration to Default Website/my_app works perfectly.

Deploying the beta configuration to Beta/my_app deploys, but when I load the site (any page) I get the incorrect format exception.

I cannot understand why deploying with the exact same config settings would work for one website but not another (on the same webserver). My server + dev machines are 64 bit, and both websites have the same app pool settings (.NET 4, integrated).

How can I find/fix this problem? Preferably without having to have different config settings for each site. And preferably without having to change the release config since that is working and I don't want to risk changing it.

The two websites are:

Default Website/my_app
Beta/my_app

The configuration manager for release has these settings:

release build configuration

The configuration manager for beta has the exact same settings:

beta build configuration

The exception is:

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

Stack Trace:

[BadImageFormatException: Could not load file or assembly 'MyApp.Domain.Model' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   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) +123

[ConfigurationErrorsException: Could not load file or assembly 'MyApp.Domain.Model' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.Configuration.CompilationSection.LoadAssemblyHelper(String assemblyName, Boolean starDirective) +11479520
   System.Web.Configuration.CompilationSection.LoadAllAssembliesFromAppDomainBinDirectory() +484
   System.Web.Configuration.AssemblyInfo.get_AssemblyInternal() +79
   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 'MyApp.Domain.Model' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11612256
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4842149

I've seen that there are multiple questions on SO asking about "An attempt was made to load a program with an incorrect format" but I do not believe this to be a duplicate. This is a unique situation with the config working fine on one site but not on another using the same config settings and the same webserver for both.


Solution 1:

Found the problem - The solution is in the way that the two AppPools are configured:

  • Default Website/my_app is using DefaultAppPool where Enable 32-Bit applications is TRUE
  • Beta/my_app -> BetaAppPool is using Enable 32-Bit applications is FALSE

Changing BetaAppPool to set Enable 32-Bit applications to TRUE has fixed this problem.

Solution was found by @Rick on this question: C# Entity Framework 4 Common Language Runtime detected an invalid program error?