.net System.BadImageFormatException 尝试加载格式不正确的程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4340362/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:59:48  来源:igfitidea点击:

System.BadImageFormatException An attempt was made to load a program with an incorrect format

.netbadimageformatexceptionrevittarget-frameworkrevit-api

提问by Eric Anastas

I'm writing a plug-in for another program that is based on a public .NET API. Typically these plugins are made by creating a class library DLL that references the API assembly. Then a command class is created by inheriting from a base command class in the API assembly. The application is then set to reference the plug-in DLL file, and is then also responsible for actually firing up the custom command class when the user requests it.

我正在为另一个基于公共 .NET API 的程序编写插件。通常,这些插件是通过创建引用 API 程序集的类库 DLL 来制作的。然后通过从 API 程序集中的基本命令类继承来创建命令类。然后,应用程序被设置为引用插件 DLL 文件,然后还负责在用户请求时实际启动自定义命令类。

However, now I'm trying to automate some code generation through System.CodeDOM, and want to create a simple console application that automatically generates new Class Types based off of types with in the API assembly.

但是,现在我正在尝试通过 自动生成一些代码System.CodeDOM,并希望创建一个简单的控制台应用程序,该应用程序根据 API 程序集中的类型自动生成新的类类型。

Yet, when I try to run my application I get the following exception.

然而,当我尝试运行我的应用程序时,我收到以下异常。

System.BadImageFormatException was unhandled Message: Could not load file or assembly 'RevitAPI, Version=2011.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.

System.BadImageFormatException 未处理消息:无法加载文件或程序集“RevitAPI,版本=2011.0.0.0,Culture=neutral,PublicKeyToken=null”或其依赖项之一。试图加载格式不正确的程序。

Usually I need to set the target framework of a plug-in assembly to 3.5. Yet now I've found that the error above goes away if I set the target framework of my console application to 2.0. However, my console application already references other class libraries of mine that have their target framework set to 3.5. And I'd really rather not rewrite them around the 2.0 framework.

通常我需要将插件程序集的目标框架设置为3.5。但是现在我发现如果我将控制台应用程序的目标框架设置为 2.0,上面的错误就会消失。但是,我的控制台应用程序已经引用了我的其他类库,这些类库的目标框架设置为 3.5。而且我真的宁愿不围绕 2.0 框架重写它们。

回答by Simon Mourier

It's possibly a 32 - 64 bits mismatch.

这可能是 32 - 64 位不匹配。

If you're running on a 64-bit OS, the Assembly RevitAPI may be compiled as 32-bit and your process as 64-bit or "Any CPU".

如果您在 64 位操作系统上运行,则 Assembly RevitAPI 可能会编译为 32 位,而您的进程可能会编译为 64 位或“任何 CPU”。

Or, the RevitAPI is compiled as 64-bit and your process is compiled as 32-bit or "Any CPU" and running on a 32-bit OS.

或者,RevitAPI 编译为 64 位,您的进程编译为 32 位或“任何 CPU”并在 32 位操作系统上运行。

回答by Yazad Gandhi

If you use IIS, Go to the Application pool Select the one that your site uses and click Advance Settings Make sure that the Enable 32-Bit Applications is set to True

如果您使用 IIS,请转到应用程序池 选择您的站点使用的应用程序池,然后单击高级设置 确保将启用 32 位应用程序设置为 True

回答by Liam

I was having problems with a new install of VS with an x64 project - for Visual Studio 2013, Visual Studio 2015 and Visual Studio 2017:

我在使用 x64 项目新安装 VS 时遇到问题 - 对于 Visual Studio 2013、Visual Studio 2015 和 Visual Studio 2017:

Tools
  -> Options
   -> Projects and Solutions
    -> Web Projects
     -> Check "Use the 64 bit version of IIS Express for web sites and projects"

回答by birwin

These suggestions are accurate, but I wanted to add a note. I was stuck simply because I had multiple publishing configurations. I was editing the "Debug - Any CPU" and then deploying the "Debug - x64" configuration. Make sure you are editing and deploying the same configuration. Verify this by clicking the "Settings" tab after you begin publishing and the "Publish Web" dialog pops up. Make sure it matches the configuration you edited. (That's 4 hours of my life I will never get back!)

这些建议是准确的,但我想添加一个注释。我被卡住了,因为我有多个发布配置。我正在编辑“调试 - 任何 CPU”,然后部署“调试 - x64”配置。确保您正在编辑和部署相同的配置。开始发布并弹出“发布网站”对话框后,通过单击“设置”选项卡来验证这一点。确保它与您编辑的配置相匹配。(那是我生命中的 4 个小时,我永远不会回来!)

回答by Athar Sayed

i have same problem what i did i just downloaded 32-bit dll and added it to my bin folder this is solved my problem

我有同样的问题我刚刚下载了 32 位 dll 并将其添加到我的 bin 文件夹这解决了我的问题

回答by Michael

I had the same issue when getting my software running on another machine. On my developer pc (Windows 7), I had Visual Studio 2015 installed, the target pc was a clean installation of Windows 10 (.Net installed). I also tested it on another clean Windows 7 pc including .Net Framework. However, on both target pc's I needed to install the Visual C++ Redistributable for Visual Studio 2015package for x86 or x64 (depends on what your application is build for). That was already installed on my developer pc.

我在另一台机器上运行我的软件时遇到了同样的问题。在我的开发人员电脑(Windows 7)上,我安装了 Visual Studio 2015,目标电脑是 Windows 10 的全新安装(安装了 .Net)。我还在另一台干净的 Windows 7 电脑上测试了它,包括 .Net Framework。但是,在两台目标电脑上,我都需要为 x86 或 x64安装Visual C++ Redistributable for Visual Studio 2015包(取决于您的应用程序的构建目的)。那已经安装在我的开发人员电脑上。

My application was using a C library, which has been compiled to a C++ application using /clr and /TP options in visual studio. Also the application was providing functions to C# by using dllexport method signatures. Not sure if the C# integration leaded to give me that error or if a C++ application would have given me the same.

我的应用程序使用的是 C 库,该库已在 Visual Studio 中使用 /clr 和 /TP 选项编译为 C++ 应用程序。该应用程序还通过使用 dllexport 方法签名向 C# 提供函数。不确定 C# 集成是否导致给我那个错误,或者 C++ 应用程序是否会给我同样的错误。

Hope it helps anybody.

希望它可以帮助任何人。