c# - 系统.BadImageFormatException : 64bit MVC 2 project - with step by step instruction to reproduce the error

标签 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 要求创建一个)

到目前为止,“MyMvcApplication”正在正常运行。即使将其设置为 x64。

  1. 我在解决方案(类库)中添加了一个名为“MyClassLibrary”的新项目
  2. 我将“MyMvcApplication”设置为起始项目(它应该已经是,但只是为了完整列出我所做的)。
  3. 我转到菜单构建/配置管理器,通过从“任何 CPU”复制来创建一个新平台“x64”。然后我将两个项目都设置为 x64。
  4. 现在我有了调试 | x64 和“MyClassLibrary”和“MyMvcApplication”平台到 x64 和“构建框”勾选。
  5. 我添加了从“MyMvcApplication”到“MyClassLibrary”的引用(添加引用,转到“项目”选项卡并选择“MyClassLibrary”)。

我运行“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”,就像 bobbymcr 在 vi​​sual studio 命令提示符中建议的那样,我得到:

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

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

编辑2:

将所有项目切换到 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 都会出现错误。

我能做什么?

最佳答案

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

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

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

关于c# - 系统.BadImageFormatException : 64bit MVC 2 project - with step by step instruction to reproduce the error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5284072/

相关文章:

c# - Windows 服务器服务总线。请求 URI 格式不正确

c# - 标识为 [x] 的项目已存在于元数据集合中。我该如何解决?

wcf - 为什么在 ASP.NET MVC 中有 JsonResult 时使用 WCF REST?

c# - 扩展 MVC RequiredAttribute

c# Directory.CreateDirectory( path ),我应该先检查路径是否存在吗?

c# - ComboBox SelectedText,为什么不切换到SelectedText项?

javascript - C# 中 JavaScript 中的变量

android - 在 Uno.Platform 中从 ViewModels 设置位图不适用于 Android

c# - 检查datagridview的单元格中的数据是否为空

asp.net - MVC2 中 Html.Partial(view, model) 和 Html.RenderPartial(view,model) 之间有什么区别(如果有)?