在发布模式下构建时出现 System.BadImageFormatException 错误

6

我在一个dll中引用了一个exe文件。在DEBUG模式下运行一切正常,但在Release模式下运行时会抛出以下异常:

System.BadImageFormatException occurred
  HResult=-2147024885
  Message=Could not load file or assembly 'Presensoft.InlineMarker, 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=Presensoft.ApplicationServer
  FileName=Presensoft.InlineMarker, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
  FusionLog==== Pre-bind state information ===
LOG: DisplayName = Presensoft.InlineMarker, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
 (Fully-specified)
LOG: Appbase = file:///F:/PresensoftNewTrunk/Trunk/Email Archiver/EmailService/Presensoft.ApplicationServerHost/bin/Release/
LOG: Initial PrivatePath = NULL
Calling assembly : Presensoft.ApplicationServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: F:\PresensoftNewTrunk\Trunk\Email Archiver\EmailService\Presensoft.ApplicationServerHost\bin\Release\Presensoft.ApplicationServerHost.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:///F:/PresensoftNewTrunk/Trunk/Email Archiver/EmailService/Presensoft.ApplicationServerHost/bin/Release/Presensoft.InlineMarker.DLL.
LOG: Attempting download of new URL file:///F:/PresensoftNewTrunk/Trunk/Email Archiver/EmailService/Presensoft.ApplicationServerHost/bin/Release/Presensoft.InlineMarker/Presensoft.InlineMarker.DLL.
LOG: Attempting download of new URL file:///F:/PresensoftNewTrunk/Trunk/Email Archiver/EmailService/Presensoft.ApplicationServerHost/bin/Release/Presensoft.InlineMarker.EXE.
ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.

  StackTrace:
       at Presensoft.ApplicationServer.ExchangeServer2010Push.PostProcessingEmailsQueue()
       at Presensoft.ApplicationServer.ExchangeServer2010Push.RunPushService(Guid appServerID) in f:\PresensoftNewTrunk\Trunk\Email Archiver\EmailService\Presensoft.ApplicationServer\ExchangeServer2010Push.cs:line 48
  InnerException: 

不确定在发布模式下运行时是否存在探测可执行文件的问题。

2个回答

10

我认为,这不是一个“Debug / Release”模式的问题,而是32位和64位的问题。可能您在调试模式下的平台目标设置为自动,并且在发布模式下设置为32/64位,并且您正在使用一个只有32位或64位的dll文件,所以在调试(平台目标自动)模式下,.NET可以“选择”32位和64位以与dll兼容,而在发布(平台目标32或64位)模式下则无法这样做。

请注意,如果您在同一解决方案中有多个项目,则必须检查所有项目的属性!它在每个项目的属性中,构建,平台目标。


可能是反过来的。假设 OP 有一个64位进程,而exe只有32位,当DEBUG设置为32位,RELEASE设置为自动时,就会出现这个问题。 - Kris Vandermotten
我已经检查了每个项目的属性,它们都是相同的。但是当我打开配置管理器时,位数有所不同。谢谢! - Sameer
我只是想指出。我为了解决这个错误而战斗了数小时。我除安装 VS 2017 外没有做任何更改,一切都正常工作。我正在使用 2015 版本。几个小时后回来,就出现了这个错误。我一遍又一遍地清理了解决方案并重新构建。反复从 TFS 获取最新版本,最终在第十次获取最新版后突然奇迹般地开始工作了。最后一次检入是我的。我真的认为 VS 中有一个 bug。 - user3478586
1
忘记配置管理器吧。对我来说,那里的一切都是相同的 - 所有的 AnyCPU,在 Debug 和 Release 中都是如此。但在项目属性 - 构建 - 目标平台中,Release 中设置了“优先使用 32 位”标志 :-/ - Jessica
由于某种原因,“prefer 32 bit”也在我的Release模式中设置了...真是太令人沮丧了。 - 00jt

1
我昨天也遇到了类似的问题。调试和发布模式都设置为AnyCPU,但exe文件引用了一个32位dll。在调试时,exe似乎默认为32位(我猜是因为Visual Studio是32位),但在发布模式下,它会切换到64位(我猜是因为我的电脑是64位)。因此,当我强制它始终为32位(x86)时,一切都应该并且确实很好!希望这有所帮助!

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