System.BadImageFormatException:64位MVC 2项目 - 使用逐步指令重现错误

时间:2011-03-12 17:15:12

标签: c# .net asp.net-mvc-2 .net-4.0 c#-4.0

我想创建一个64位ASP.NET MVC 2 Web应用程序,并使用64位类库引用它。 但这样做我得到一个“System.BadImageFormatException”-Exception

我添加的每个项目/解决方案都是.Net 4.0:

  1. 我创建了一个名为“MySolution”的新空白解决方案
  2. 我向该解决方案添加了一个新项目(ASP.NET MVC 2 Web应用程序),名为“MyMvcApplication”,没有测试单元使其更容易(Visual Studio要求创建一个)
  3. 到目前为止,“MyMvcApplication”正在运行。即使将其设置为x64。

    1. 我在soltion(类库)中添加了一个名为“MyClassLibrary”的新项目
    2. 我将“MyMvcApplication”设置为启动项目(它应该已经存在,但只是为了完整列表我所做的事情)。
    3. 我转到菜单Build / Configuration Manager,通过从“Any CPU”复制创建一个新的平台“x64”。然后我将两个项目都设置为x64。
    4. 现在我有Debug | x64和“MyClassLibrary”和“MyMvcApplication”平台到x64和“构建框”勾选。
    5. 我将“MyMvcApplication”中的引用添加到“MyClassLibrary”(添加引用,进入“项目”选项卡并选择“MyClassLibrary”)。
    6. 我运行“MyMvcApplication”并收到以下错误:

      Could not load file or assembly 'MyClassLibrary' 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
      'MyClassLibrary' 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 'MyClassLibrary' could not be loaded.
      
      === Pre-bind state information ===
      LOG: User = Dev\Chris
      LOG: DisplayName = MyClassLibrary
       (Partial)
      WRN: Partial binding information was supplied for an assembly:
      WRN: Assembly Name: MyClassLibrary | Domain ID: 2
      WRN: A partial bind occurs when only part of the assembly display name is provided.
      WRN: This might result in the binder loading an incorrect assembly.
      WRN: It is recommended to provide a fully specified textual identity for the assembly,
      WRN: that consists of the simple name, version, culture, and public key token.
      WRN: See whitepaper http://go.microsoft.com/fwlink/?LinkId=109270 for more information and common solutions to this issue.
      LOG: Appbase = file:///c:/Test/MySolution/MyMvcApplication/
      LOG: Initial PrivatePath = c:\Test\MySolution\MyMvcApplication\bin
      Calling assembly : (Unknown).
      ===
      LOG: This bind starts in default load context.
      LOG: Using application configuration file: c:\Test\MySolution\MyMvcApplication\web.config
      LOG: Using host configuration file: 
      LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
      LOG: Policy not being applied to reference at this time (private, custom, partial, or location-based assembly bind).
      LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/f962adb4/1764faec/MyClassLibrary.DLL.
      LOG: Attempting download of new URL file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/Temporary ASP.NET Files/root/f962adb4/1764faec/MyClassLibrary/MyClassLibrary.DLL.
      LOG: Attempting download of new URL file:///c:/Test/MySolution/MyMvcApplication/bin/MyClassLibrary.DLL.
      ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
      

      编辑:

      运行“corflags c:\ Test \ MySolution \ MyMvcApplication \ bin \ MyClassLibrary.DLL”,就像在visual studio命令提示符中的bobbymcr建议我得到:

      Version   : v4.0.30319
      CLR Header: 2.5
      PE        : PE32+
      CorFlags  : 1
      ILONLY    : 1
      32BIT     : 0
      Signed    : 0
      

      但我不知道如何处理这些信息: - (

      EDIT2:

      将所有项目切换到x86并运行ASP.NET MVC 2 Web应用程序时,出现以下错误:

      Parser Error 
      Description: An error occurred during the parsing of a resource required to service this
      request. Please review the following specific parse error details and modify your
      source file appropriately. 
      
      Parser Error Message: Could not load type 'MyMvcApplication.MvcApplication'.
      
      Source Error: 
      
      Line 1:  <%@ Application Codebehind="Global.asax.cs"
      Inherits="MyMvcApplication.MvcApplication" Language="C#" %>
      
      
      Source File: /global.asax    Line: 1
      

      所以无论是否切换到x86或x64,我都会收到错误。

      我该怎么办?

1 个答案:

答案 0 :(得分:4)

事实是x64位应用程序可以加载x86和x64程序集。但是x86应用程序只能加载x86程序集。 Casini(Visual Studio Development Server)是x86应用程序,这就是它不支持运行x64 Web应用程序的原因。您必须仅在支持x86和x64的IIS上托管x64 Web应用程序。

还要确保您的类库的Target平台是“.NET Framework 4.0”,而不是“.Net 4.0 Client profile”。

如果问题解决了,请将此帖标记为答案。