ASP.NET -- IIS7 -- IBM DB2问题

11

我正在开发一个调用DB2数据库的ASP.NET网站。我在Windows 2008服务器上安装了Visual Studio,该站点将在此处托管。当我使用集成的Web服务器在Visual Studio中调试站点时,我可以连接到数据库并成功运行站点。但是,在同一台服务器上设置站点在IIS7中时,由于以下错误,我无法访问站点或数据库。

以下是技术细节:

Windows 2008 Server
IIS 7
Visual Studio 2010 Premium
DB2 v9.5.301.436

Could not load file or assembly 'IBM.Data.DB2' or one of its dependencies. An attempt was made to load a program with an incorrect format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.BadImageFormatException: Could not load file or assembly 'IBM.Data.DB2' or one of its dependencies. An attempt was made to load a program with an incorrect format.

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

Assembly Load Trace: The following information can be helpful to determine why the assembly 'IBM.Data.DB2' could not be loaded.

WRN: Assembly binding logging is turned OFF.
To enable assembly bind failure logging, set the registry value [HKLM\Software\Microsoft\Fusion!EnableLog] (DWORD) to 1.
Note: There is some performance penalty associated with assembly bind failure logging.
To turn this feature off, remove the registry value [HKLM\Software\Microsoft\Fusion!EnableLog]. 

Stack Trace: 

[BadImageFormatException: Could not load file or assembly 'IBM.Data.DB2' 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) +118

[ConfigurationErrorsException: Could not load file or assembly 'IBM.Data.DB2' 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) +11424435
   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) +1167

[HttpException (0x80004005): Could not load file or assembly 'IBM.Data.DB2' or one of its dependencies. An attempt was made to load a program with an incorrect format.]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +11556592
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +141
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +4813333

我不明白为什么网站在使用Visual Studio Web服务器时可以正常运行,但在IIS中无法工作。我不知道这是一个IIS问题还是我正在使用的IMB.Data.DB2.dll版本的问题。如果有人有解决方案或任何想法,将不胜感激。

谢谢。

4个回答

17
为使此项工作顺利进行,我需要进入IIS管理器,点击应用程序池节点,点击站点的应用程序池,然后在屏幕右侧点击高级设置。接着,将“启用32位应用程序”更改为true。
这样就解决了我的问题,网站现在可以正常运行。

2
这会修复错误,但是强制应用程序在32位下运行。真正的解决方案是从bin文件夹中删除32位的IBM.Data.DB2.dll,这样IIS就可以在全局程序集缓存中找到正确的(64位)版本。 - Marc Selis
@MarcSelis 非常感谢,你的评论帮了我很多 :) - Sibeesh Venu

6

由于某种奇怪的原因,Visual Studio 2012(以及更高版本)始终会将32位版本的IBM.Data.DB2.dll复制到您的bin文件夹中,尽管安装DB2 Connect时该DLL已在GAC中注册,因此不需要它。

最简单的解决方法是添加一个后置构建脚本,在每次构建后从bin文件夹中删除该DLL。这样,IIS将能够在GAC中找到正确的版本(是否需要32位或64位版本取决于您的AppPool设置)。


谢谢,这解决了我的问题。我会看看能否防止最初的复制发生。顺便说一下,这个问题在Visual Studio 2015中也存在。 - DevOhrion
我把 DLL 文件复制到了 bin 目录下。当我将其删除后,这个错误就不再出现了。 - Philip Rego

1

0

看起来你的 AppDomain 中 .Net 框架版本不正确。在 AppDomain 设置中,确保所选的 .Net 框架版本与你的 IBM.Data.DB2 程序集的版本相匹配。


谢谢您的回复。我尝试了这个方法,但是没有成功。不过我已经找到了问题所在。 - user718300

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接