System.BadImageFormatException 在发布模式下构建时发生

System.BadImageFormatException occurred when build in Release Mode

我在 dll 中引用了一个 exe 文件。当 运行 在 DEBUG 模式 时一切 运行 都很好但是当 运行 在 发布模式 下面的异常是抛出

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: 

不确定在 RELEASE 模式下运行探测 exe 文件是否有问题。

不仅仅是 Debug/Release 模式,我会说这是一个 32 位与 64 位的问题。可能您将 Platform target 的调试模式设置为自动,将发布模式设置为 32/64 位,并且您使用的 dll 仅为 32 位或 64 位,因此在调试 (平台目标 Auto) 模式 .NET 可以 "select" 在 32 和 64 位之间与 dll 兼容,而在 Release 中(平台目标 32 或 64 位)它不能。

(请注意,如果您在同一个解决方案中有多个项目,则必须为此检查所有项目属性!它位于每个项目、Build、Platform 目标的属性中)

我 运行 昨天也进入了这个(或类似的东西)。 Debug 和 Release 模式都设置为 AnyCPU,但 exe 引用的是 32 位 dll。调试时,exe 似乎默认为 32 位(我假设是因为 Visual Studio 是 32 位),但在发布模式下,它切换到 64 位(我假设是因为我的计算机是 64 位)。因此,当我强制它始终为 32 位 (x86) 时,一切都应该而且确实运行良好!希望这对您有所帮助!