【问题标题】:ASP.NET -- IIS7 -- IBM DB2 IssueASP.NET -- IIS7 -- IBM DB2 问题
【发布时间】:2011-06-29 17:03:00
【问题描述】:

我正在开发一个调用 DB2 数据库的 ASP.NET 网站。我在将托管该站点的 Windows 2008 服务器上安装了 Visual Studio。当我在 Visual Studio 中调试站点时,使用集成的 Web 服务器,我可以连接到数据库并且站点运行良好。当我在 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 版本的问题。如果有人有解决方案或任何想法,将不胜感激。

谢谢。

【问题讨论】:

    标签: asp.net iis iis-7 db2


    【解决方案1】:

    为了完成这项工作,我必须进入 IIS 管理器,单击应用程序池节点,单击站点的应用程序池,然后单击屏幕右侧的高级设置。然后我将启用 32 位应用程序更改为 true。

    这解决了我的问题,网站运行正常。

    【讨论】:

    • 这会修复错误,但会强制您的应用程序以 32 位运行。真正的解决方案是从 bin 文件夹中删除 32 位 IBM.Data.DB2.dll,以便 IIS 可以在 GAC 中找到正确的(64 位)版本。
    • @MarcSelis 非常感谢,您的评论有帮助:)
    【解决方案2】:

    出于某种奇怪的原因,Visual Studio 2012(及更高版本)总是将 32 位版本的 IBM.Data.DB2.dll 复制到您的 bin 文件夹中,即使不需要这样做,因为该 DLL 在 DB2 Connect 启用时已注册 GAC已安装。

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

    【讨论】:

    • 谢谢你,它解决了我的问题。我会看看我是否可以阻止初始复制的发生。顺便说一句,Visual Studio 2015 也会发生这种情况。
    • 我将 DLL 复制到了 bin 中。当我删除它时,我不再收到此错误。
    【解决方案3】:

    在 Visual Studio 中:右键单击有问题的引用并将 Copy Local 更改为“False” https://i.stack.imgur.com/y0Dre.png

    更多信息: IBM DB2 issue--System.BadImageFormatException: Could not load file or assembly IBM.DB2.DLL or one of its dependencies

    【讨论】:

      【解决方案4】:

      听起来您的 AppDomain 中的 .Net 框架版本有误。在 AppDomain 设置中,确保选择的 .Net 框架版本与您的 IBM.Data.DB2 程序集的版本相匹配。

      【讨论】:

      • 感谢您的回复。我试过了,但没有用。我能够找出问题所在
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-06
      相关资源
      最近更新 更多