Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 237
JK. Avatar asked Jul 18 '11 03:07

JK.


People also ask

How do you fix an attempt was made to load a program with an incorrect format exception from Hresult 0x8007000B?

(Exception from HRESULT: 0x8007000B) Print. Solution: This issue arises when there is a 32 bit/64 bit mismatch between the Argon2 DLL and the application that you are using. Basically you will need to ensure that if you are targeting 64 bit you're using the 64 bit binary and vice versa.

How do you fix an attempt was made to load a program with an incorrect format?

Simply right-click on Project > go to Properties > Build > check Prefer 32-bit. You can also use preprocessor conditions, but in this case you need to compile two different assemblies. In other words, compile a 32-bit assembly for 32-bit platforms, and a separate 64-bit assembly for 64-bit platforms.


1 Answers

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?

like image 137
JK. Avatar answered Sep 22 '22 17:09

JK.