故障排除BadImageFormatException

129

我有一个使用Visual Studio 2010编写的基于C#语言、针对.NET Framework 4.0的Windows服务。在调试版本下可以正常运行,但是在发布版本下遇到了System.BadImageFormatException异常(以下为详细信息)。我已经在互联网上搜索了很多解决方案,但至今没有找到合适的。

这个问题在Windows 7 64位系统和Windows XP SP3 32位系统中都存在。

以下是我尝试过的方法:

  • 验证生成设置,例如平台目标是否相同(x86)。
  • 使用带有/verbose选项的peverify确保程序集二进制文件有效。
  • 使用fuslogvw查找任何加载问题。
  • 使用CheckAsm查找缺失的文件或程序集。

所有这些检查都没有改变任何东西。我在下面附上了异常信息的全部文本,其中一些名称已更改以保护我的公司机密。

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. ===
日志:此绑定在默认加载上下文中开始。 日志:使用应用程序配置文件:c:\ TeamE \ bin \ Release \ XxxDevicesService.vshost.exe.Config 日志:使用主机配置文件: 日志:使用计算机配置文件C:\ Windows \ Microsoft.NET \ Framework64 \ v4.0.30319 \ config \ machine.config。 日志:此时未将策略应用于引用(私有、自定义、部分或基于位置的程序集绑定)。 日志:尝试下载新的URL文件:///c:/TeamE/bin/Release/XxxDevices.DLL。 错误:无法完成程序集设置(hr = 0x8007000b)。探测终止。
StackTrace: 在XxxDevicesService.Program.Main(String[] args) 在System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) 在System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在System.Threading.ThreadHelper.ThreadStart() InnerException:

你是否混合使用本地代码和.NET? - Keith Nicholas
1
你的想法是正确的,这个异常与x86/x64位差异有关。我猜这不是一个Web应用程序,对吗?另外,XxxDevicesService是什么类型的程序集?它是否为特定平台编译(例如32位)?如果是这样,那么你必须将你的平台编译成32位。 - Reddog
https://support.microsoft.com/en-us/kb/967163 - Palec
如果这些答案解决了您的问题,请注意使用的“WebView2Loader.dll”的版本非常重要。当我尝试在MMC Snap-Ins中使用HTMLView或FormView类型的WebView2组件时,我遇到了与“Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64”相同的问题。 - amirfg
我只是复制了上述的dll文件(版本1.0.1248.0,大小=157640字节)到一个适当的路径,该路径对项目可访问(您可以先将其放在项目输出文件旁边进行测试),然后WebView2浏览器开始按预期工作。微软的错误消息有时(至少在我的情况下)有点误导,并没有传达足够和到位的信息。 - amirfg
我收到了“BadImageFormatException”错误,通常发生在混合平台目标(例如,在针对x86或反之亦然的应用程序中使用在X64中编译的dll文件)或混合本机代码和.NET时,但这并不是我的问题所在。我希望这能帮助那些可能遇到类似问题的人。 - amirfg
25个回答

0
当我遇到这个问题时,以下方法解决了它:
我正在从另一个exe文件中调用OpenCV dll,我的dll没有包含已经需要的opencv dll,如highgui、features2d等,这些dll在我的exe文件夹中可用。我将所有这些复制到我的exe项目目录中,然后它突然就可以工作了。

0
请注意,“WebView2Loader.dll”的版本非常重要。当我尝试在MMC Snap-Ins中使用“HTMLView”或“FormView”类型的WebView2组件时,我几乎遇到了与“Microsoft.WebView2.FixedVersionRuntime.101.0.1210.39.x64”相同的问题。
我只需将上述dll文件(版本1.0.1248.0,大小=157640字节)复制到项目可访问的正确路径中(您可以先将其放在项目输出文件旁边进行测试),然后WebView2浏览器就开始按预期工作了。微软的错误消息有时(至少在我的情况下)有点误导,并没有提供足够明确的信息。
我收到了“BadImageFormatException”错误,通常发生在混合平台目标(例如在面向x86或反之的应用程序中使用在X64编译的dll文件)或混合本机代码和.NET时,但这并不是我的问题。希望这能帮助那些可能会遇到类似问题的人。

0

这个错误 "无法加载文件或程序集 'example' 或其某个依赖项。尝试加载格式不正确的程序" 通常是由于应用程序池配置不正确引起的。

  1. 确保您网站当前运行的 AppPool 的 "启用 32 位应用程序" 设置为 False。
  2. 确保您正在使用适合您平台的正确版本。
  3. 如果您在 Web 站点上遇到此错误,请确保您的应用程序池设置为以正确模式运行(3.0 站点应以 64 位模式运行)。
  4. 您还应确保 Visual Studio 中对该程序集的引用指向包文件夹中的正确文件。
  5. 确保您在 GAC 中安装了适用于 2.0 站点的正确版本的 dll。
  6. 这也可能是由于 WSODLibs 与 Web 项目一起推广而引起的。

0

如果有人在没有安装VS的目标机器上遇到Visual C++异常,可以尝试以下方法:

  1. VC++需要运行时
  2. 所有代码都应该编译为Release !!!

0

关于 CI/CDMSBuildDevEnv

您的构建机器日志显示 - MSB3270 ... MSIL vs AMD64。

warning MSB3270: There was a mismatch between the processor architecture of the project
  being built "MSIL" and the processor architecture of the reference
  "C:\build-machine\my-solution\My-1\bin\Release\My-1.dll", "AMD64".
This mismatch may cause runtime failures.
Please consider changing the targeted processor architecture of your project through
  the Configuration Manager so as to align the processor architectures between your
  project and references, or take a dependency on references with a processor architecture
  that matches the targeted processor architecture of your project. [C:\build-machine\my-solution\My-2.csproj]

在您的站点的IIS应用程序池高级设置中,您已将启用32位应用程序值设置为True。

部署后,您的站点显示以下错误:

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

您可以通过将启用32位应用程序设置为False来解决此问题,但您必须使用32位模式。

检查和修复步骤

以下步骤中的某些步骤肯定会对您有所帮助。

*. 在VSCode中打开您的Visual Studio解决方案文件夹,并搜索类似于此的内容

<Target Name="AfterBuild">
    <MSBuild Condition=" '$(Platform)' == 'x86' " Projects="$(MSBuildProjectFile)"
        Properties="Platform=x64;PlatFormTarget=x64" RunEachTargetSeparately="true" />
</Target>

考虑从项目文件(.csproj)中删除它。

*. 在VS中打开您的解决方案并打开配置管理器。
考虑从所有解决方案配置中删除x64平台。

*. 对于那些需要逐个构建项目的人。
考虑在运行Any Cpu(MSIL)、x86项目之前,将构建顺序更改为先构建x64项目。或者为不同平台配置项目的输出路径
在创建部署包时要小心。考虑在项目构建运行之间累积不同的构建输出。

*. 考虑从应该有x64平台的项目文件中清除所有x64相关内容。就像这样:

<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
    ....
</PropertyGroup>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接