ASP.NET -- IIS7 -- IBM DB2 Issue
I'm working on a ASP.NET website that calls a DB2 database. I have Visual Studio installed on a Windows 2008 server where the site will be hosted. When I debug the site in Visual Studio, using the integrated web server, I can connect to the database and the site runs fine. When I set the site up in IIS7, on the same server, I cannot access the site or the database because of the following error.
Here are the technical details:
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.LoadAssem开发者_JAVA百科blyHelper(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
I don't understand how the site can run fine using the Visual Studio web server and not work in IIS. I don't know if it's an IIS issue or and issue with the version of IMB.Data.DB2.dll I'm using. If anyone has a solution or any ideas, they would be greatly appreciated.
Thank you.
To make this work I had to go into IIS Manager, click on the application pool node, click on the application pool for the site, then click on advanced settings on the right hand side of the screen. Then I changed Enable 32 bit applications to true.
This fixed my issue and the site is functioning correctly.
For some bizarre reason Visual Studio 2012 (and later) always copies the 32-bit version of IBM.Data.DB2.dll to your bin folder, even though that is not needed because that DLL is registered GAC when DB2 Connect is installed.
The simplest solution is to add a post-build script to delete that DLL from your bin folder after each build. That way IIS will be able to find the correct version (whether it needs the 32-bit or 64-bit version depends from your AppPool settings) in the GAC.
In Visual Studio: right click the Reference in question and change Copy Local to "False" https://i.stack.imgur.com/y0Dre.png
More info: IBM DB2 issue--System.BadImageFormatException: Could not load file or assembly IBM.DB2.DLL or one of its dependencies
It sounds like the .Net framework version is wrong in your AppDomain. In the AppDomain settings, make sure the .Net framework version that is selected matches the version of your IBM.Data.DB2 assembly.
精彩评论