Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
129 views
in Technique[技术] by (71.8m points)

c# - 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.

question from:https://stackoverflow.com/questions/6728339/iis-7-5-fixing-an-attempt-was-made-to-load-a-program-with-an-incorrect-format-pr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...