C# BadImageFormatException 故障排除

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8996653/
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-08-09 05:30:39  来源:igfitidea点击:

Troubleshooting BadImageFormatException

c#.netexception

提问by

I have a Windows service written in C# using Visual Studio 2010 and targeting the full .NET Framework 4. When I run from a Debug build the service runs as expected. However, when I run it from a Release build I get a System.BadImageFormatException (details below). I've been searching the internet for a solution but so far every thing I've found hasn't helped me find a solution.

我有一个使用 Visual Studio 2010 用 C# 编写的 Windows 服务,目标是完整的 .NET Framework 4。当我从调试版本运行时,服务按预期运行。但是,当我从 Release 版本运行它时,我得到一个 System.BadImageFormatException(详细信息如下)。我一直在互联网上寻找解决方案,但到目前为止,我发现的每件事都没有帮助我找到解决方案。

The problem exists on both Windows 7 64-bit (dev) and Windows XP SP3 32-bit (target) systems.

该问题存在于 Windows 7 64 位(开发)和 Windows XP SP3 32 位(目标)系统上。

Here is what I've tried so far:

这是我迄今为止尝试过的:

  • Verified build settings such as Platform Target are all the same (x86).
  • Used peverify with the /verbose option to ensure the assembly binaries were valid.
  • Uses fuslogvw to look for any loading issues.
  • Used CheckAsm to look for missing files or assembiles.
  • 经过验证的构建设置(例如 Platform Target)都是相同的 (x86)。
  • 将 peverify 与 /verbose 选项一起使用以确保程序集二进制文件有效。
  • 使用 fuslogvw 查找任何加载问题。
  • 使用 CheckAsm 查找丢失的文件或程序集。

All of these checks didn't change anything. I've included the full text of the exception information below, with some of the names changed to protect the secrets of my corporate masters.

所有这些检查都没有改变任何东西。我在下面包含了例外信息的全文,其中一些名称已更改以保护我公司主人的秘密。

System.BadImageFormatException was unhandled
  Message=Could not load file or assembly 'XxxDevices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  Source=XxxDevicesService
  FileName=XxxDevices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog=Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  c:\Dev\TeamE\bin\Release\XxxDevicesService.vshost.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: User = XXX
LOG: DisplayName = XxxDevices, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///c:/Dev/TeamE/bin/Release/
LOG: Initial PrivatePath = NULL
Calling assembly : XxxDevicesService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: c:\TeamE\bin\Release\XxxDevicesService.vshost.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\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:/TeamE/bin/Release/XxxDevices.DLL.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

  StackTrace:
       at XxxDevicesService.Program.Main(String[] args)
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 

采纳答案by Hans Passant

Verified build settings such as Platform Target are all the same (x86).

经过验证的构建设置(例如 Platform Target)都是相同的 (x86)。

That's not what the crash log says:

那不是崩溃日志所说的:

Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64

从以下位置加载的程序集管理器:C:\Windows\Microsoft.NET\Framework64

Note the 64in the name, that's the home of the 64-bit version of the framework. Set the Target platform setting on your EXEproject, not your class library project. The XxxDevicesService EXE project determines the bitness of the process.

请注意名称中的64,这是 64 位版本框架的所在地。在您的EXE项目上设置目标平台设置,而不是您的类库项目。XxxDevicesService EXE 项目决定了进程的位数。

回答by purvin

It can typically occur when you changed the target framework of .csproj and reverted it back to what you started with.

当您更改 .csproj 的目标框架并将其恢复为开始时,通常会发生这种情况。

Make sure 1 if supportedRuntime version="a different runtime from cs project target" under startup tag in app.config.

确保 1 if supportedRuntime version="a different runtime from cs project target" 在 app.config 的启动标签下。

Make sure 2 That also means checking other autogenerated or other files in may be properties folder to see if there is no more runtime mismatch between these files and one that is defined in .csproj file.

确保 2 这也意味着检查可能是属性文件夹中的其他自动生成或其他文件,以查看这些文件与 .csproj 文件中定义的文件之间是否不再存在运行时不匹配。

These might just save you lot of time before you start trying different things with project properties to overcome the error.

在您开始尝试不同的项目属性以克服错误之前,这些可能会为您节省大量时间。

回答by S.N.

I had the same problem even though I have 64-bit Windows 7 and i was loading a 64bit DLL b/c in Project properties | Build I had "Prefer 32-bit" checked. (Don't know why that's set by default). Once I unchecked that, everything ran fine

即使我有 64 位 Windows 7 并且我在项目属性中加载了 64 位 DLL b/c,我也遇到了同样的问题 | 构建我选中了“首选 32 位”。(不知道为什么默认设置)。一旦我取消选中它,一切都运行良好

回答by Cocu_1012

I fixed this issue by changing the web app to use a different "Application Pool".

我通过更改 Web 应用程序以使用不同的“应用程序池”来解决此问题。

回答by Cédric V

You can also get this exception when your application target .NET Framework 4.5 (for example) and you have the following app.config :

当您的应用程序面向 .NET Framework 4.5(例如)并且您具有以下 app.config 时,您也可能会收到此异常:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <startup>
    <supportedRuntime version="v2.0.50727" />
    <supportedRuntime version="v4.0" />
  </startup>
</configuration>

When trying to launch the debug of the application you will get the BadImageFormatException.

尝试启动应用程序的调试时,您将收到 BadImageFormatException。

Removing the line declaring the v2.0 version will clear the error.

删除声明 v2.0 版本的行将清除错误。

I had this issue recently when I tried to change the target platform from an old .NET 2.0 project to .NET 4.5.

我最近在尝试将目标平台从旧的 .NET 2.0 项目更改为 .NET 4.5 时遇到了这个问题。

回答by McSick

For anyone who may arrive here at a later time....Nothing worked for me. All my assemblies were fine. I had an app config in one of my Visual Studio Projects that shouldn't have been there. So make sure your app config file is needed.

对于可能稍后到达这里的任何人......没有对我有用。我所有的程序集都很好。我的 Visual Studio 项目之一中有一个不应该存在的应用程序配置。因此,请确保需要您的应用程序配置文件。

I deleted the extra app config and it worked.

我删除了额外的应用程序配置并且它起作用了。

回答by Anand

Determine the application pool used by the application and set the property of by setting Enable 32 bit applications to True. This can be done through advance settings of the application pool.

确定应用程序使用的应用程序池,并通过将启用 32 位应用程序设置为 True 来设置 的属性。这可以通过应用程序池的预先设置来完成。

回答by Joey Morgan

After I stopped banging my head on the desk thinking of the entire week I spent running down this problem, I am sharing what worked for me. I have Win7 64 bit, 32-bit Oracle Client, and have my MVC 5 project set to run on x86 platform because of the Oracle bitness. I kept getting the same errors:

在我停止把头撞在桌子上思考我花了整整一周的时间解决这个问题之后,我正在分享对我有用的东西。我有 Win7 64 位、32 位 Oracle 客户端,并且由于 Oracle 位数,我的 MVC 5 项目设置为在 x86 平台上运行。我不断收到相同的错误:

Could not load file or assembly 'Oracle.DataAccess' or one of its dependencies. An attempt was made to load a program with an incorrect format.

无法加载文件或程序集“Oracle.DataAccess”或其依赖项之一。试图加载格式不正确的程序。

I reloaded the NuGet packages, I used copies of the DLLs that worked for others in different apps, I set the codebase in the dependent assembly to point to my project's bin folder, I tried CopyLocal as true or false, I tried everything.Finally I had enough else done I wanted to check in my code, and as a new contractor I didn't have subversion set up. While looking for a way to hook it into VS, I tripped over the answer. What I found worked was unchecking the "Use the 64 bit version of IIS Express for Web Sites and Projects" option under the Projects and Solutions => Web Projects section under the Tools=>Options menu.

我重新加载了 NuGet 包,我使用了在不同应用程序中为其他人工作的 DLL 副本,我将依赖程序集中的代码库设置为指向我项目的 bin 文件夹,我尝试将 CopyLocal 设置为 true 或 false,我尝试了一切。最后,我做的够多了,我想检查我的代码,作为一个新的承包商,我没有设置颠覆。在寻找将其连接到 VS 的方法时,我被答案绊倒了。我发现有效的是取消选中工具 => 选项菜单下项目和解决方案 => Web 项目部分下的“为网站和项目使用 64 位版本的 IIS Express”选项。

回答by Jeremy Tinkler

When building apps for 32-bit or 64-bit platform (My experience is with Visual Studio 2010), don't rely on the Configuration Manager to set the correct platform for the executable. Even if the CM has x86 selected for the application, check the project properties (Build tab): it might still say "Any CPU" there. And if you run an "Any CPU" executable on a 64-bit platform, it will run in 64-bit mode and refuse to load your accompanying DLLs that were built for the x86 platform.

在为 32 位或 64 位平台(我的经验是使用 Visual Studio 2010)构建应用程序时,不要依赖配置管理器为可执行文件设置正确的平台。即使 CM 为应用程序选择了 x86,检查项目属性(构建选项卡):它可能仍然在那里显示“任何 CPU”。如果您在 64 位平台上运行“任何 CPU”可执行文件,它将以 64 位模式运行并拒绝加载您为 x86 平台构建的随附 DLL。

回答by Fabio

For anyone who may arrive here at a later time...
For Desktop solution I got BadImageFormatExceptionexception.
All project's build options was fine (all x86). But StartUp project of solution was changed to some other project(class library project).

对于可能稍后到达这里的任何人......
对于桌面解决方案,我有BadImageFormatException例外。
所有项目的构建选项都很好(全部x86)。但是解决方案的启动项目被更改为其他项目(类库项目)。

Changing StartUp project to the original(.exe application project) was a solution in my case

在我的情况下,将启动项目更改为原始(.exe 应用程序项目)是一个解决方案