6

I am referencing an exe file in a dll. When run in DEBUG mode everything runs fine but when run in Release mode below exception is thrown

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: 

Not sure whether there is some issue with probing exe file when running in RELEASE mode.

2 Answers 2

10

More than a Debug/Release mode I'll say it's a 32 vs 64 bits problem. Probably you have the Platform target for the Debug mode to Auto and for the Release mode to 32/64 bits and you are using a dll that is only 32 or 64 bits, so in Debug (Platform target Auto) mode the .NET can "select" between 32 and 64 bits to be compatible with the dll, while in Release (Platform target 32 or 64 bits) it can't.

(note that if you have multiple projects in the same solution you have to check all the projects properties for this! It's in the properties of each project, Build, Platform target)

5
  • It's probably the other way around. Assuming OP has a 64 bit process and the exe is 32 bit only, you get this problem when DEBUG is set to 32 bit, and RELEASE is set to auto. Commented Feb 25, 2015 at 12:48
  • I had checked the properties of each project and they were all same. But when i opened configuration manager, there were differences in the bitness. Thanks!!!
    – Sameer
    Commented Feb 25, 2015 at 12:54
  • I just wanted to note. I just fought this error for hours. I made no changes other than installing VS 2017 which everything worked fine right after. I'm using 2015. Came back after a couple hours and had this error. I cleaned the solution tons of times and rebuilt. Got latest over and over from TFS and finally out of the blue it started working after getting latest for like the 10th time. The last check-in was mine. I seriously believe there's a bug in VS. Commented Oct 5, 2017 at 0:34
  • 1
    Forget the configuration manager. For me everything was identical there - everything AnyCPU, for both Debug and Release. But in the project properties - Build - Targetplatform the flag "Prefer 32 bit" was set in Release :-/
    – Jessica
    Commented Aug 20, 2019 at 12:34
  • "prefer 32 bit" was also set for some reason in Release mode for me... sooooo frustrating.
    – 00jt
    Commented Feb 9, 2022 at 17:55
1

I ran into this (or something similar) yesterday also. Both Debug and Release mode are set to AnyCPU but the exe is referencing a 32-bit dll. While debugging, the exe appears to default to 32-bit (I assume because Visual Studio is 32-bit) but when in release mode, it switches to 64-bit (I assume because my computer is 64-bit). So when I force it to always be 32-bit (x86), everything should and does work great! Hope this helped!

Not the answer you're looking for? Browse other questions tagged or ask your own question.