title: BadImageFormatException Could not load file or assembly or one of its dependencies. An attempt was made to load a program with an incorrect format
body: <p>I am getting following runtime error, with my console application(VS2012) which refers to "dcasdk.dll".
  .Net Framework of the console app is 4.5, platform target is "Any CPU".</p>
  
  <pre><code>Could not load file or assembly 'dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
  </code></pre>
  
  <p>I used CorFlags application to check the target platform of the dll. Below are the details.. </p>
  
  <pre><code>Version : v4.0.30319
  CLR Header : 2.5
  PE : PE32
  CorFlags : 16
  ILONLY : 0
  32BIT : 0
  Signed : 0
  </code></pre>
  
  <p>As per the information above, i think the dll is also built using "Any CPU" target. So it should work with the console app having the same target platform. I am not sure why i get this error when i run.</p>
  
  <p>I also tried changing the target platform to x86 which gives the following "FileNotFoundException". I checked for the dll's references in reflector. It only shows System.Xml and System.Data. Which are already added as a reference in the console app.</p>
  
  <pre><code>Could not load file or assembly 'dcasdk.dll' or one of its dependencies. The specified module could not be found.
  </code></pre>
  
  <p>Any help regarding this will be greatly appreciated.</p>
  
  <p>Thanks in advance.</p>
  
  <p>Below is the Assembly Binding Log from Fusion.. as per the last three lines of error information, this seems to be an issue with assembly platform. But as this is a third party dll, i cannot recompile this to any specific platform. Please share your thoughts on what can be done here..</p>
  
  <p>Please note that i am running this console app on Windows7, 64bit OS.</p>
  
  <pre><code>*** Assembly Binder Log Entry  (4/26/2017 @ 8:31:08 AM) ***
  
  The operation failed.
  Bind result: hr = 0x8007000b. An attempt was made to load a program with an incorrect format.
  
  Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
  Running under executable  C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe
  --- A detailed error log follows. 
  
  === Pre-bind state information ===
  LOG: DisplayName = dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null
   (Fully-specified)
  LOG: Appbase = file:///C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/
  LOG: Initial PrivatePath = NULL
  LOG: Dynamic Base = NULL
  LOG: Cache Base = NULL
  LOG: AppName = ConsoleApplication3.vshost.exe
  Calling assembly : ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.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:/ConsoleApplication3/ConsoleApplication3/bin/Debug/dcasdk.DLL.
  LOG: Assembly download was successful. Attempting setup of file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll
  LOG: Entering run-from-source setup phase.
  LOG: Assembly Name is: dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null
  ERR: Invalid assembly platform or ContentType in file (hr = 0x8007000b).
  ERR: Run-from-source setup phase failed with hr = 0x8007000b.
  ERR: Failed to complete setup of assembly (hr = 0x8007000b). Probing terminated.
  </code></pre>
  
  <p>If i set "Prefer 32-bit" ON, then the error above disappears. However i still get "FileNotFoundException", "Could not load file or assembly 'dcasdk.dll' or one of its dependencies. The specified module could not be found.". But this time no error in Fusion logs!</p>
  
  <pre><code>*** Assembly Binder Log Entry  (4/26/2017 @ 9:57:53 AM) ***
  
  The operation was successful.
  Bind result: hr = 0x0. The operation completed successfully.
  
  Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
  Running under executable  C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe
  --- A detailed error log follows. 
  
  === Pre-bind state information ===
  LOG: DisplayName = dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null
   (Fully-specified)
  LOG: Appbase = file:///C:/ConsoleApplication3/ConsoleApplication3/bin/Debug/
  LOG: Initial PrivatePath = NULL
  LOG: Dynamic Base = NULL
  LOG: Cache Base = NULL
  LOG: AppName = ConsoleApplication3.vshost.exe
  Calling assembly : ConsoleApplication3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\ConsoleApplication3.vshost.exe.Config
  LOG: Using host configuration file: 
  LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\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:/ConsoleApplication3/ConsoleApplication3/bin/Debug/dcasdk.DLL.
  LOG: Assembly download was successful. Attempting setup of file: C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll
  LOG: Entering run-from-source setup phase.
  LOG: Assembly Name is: dcasdk, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null
  LOG: Binding succeeds. Returns assembly from C:\ConsoleApplication3\ConsoleApplication3\bin\Debug\dcasdk.dll.
  LOG: Assembly is loaded in default load context.
  </code></pre>
  
  <p>"FileNotFoundException" stacktrace..</p>
  
  <pre><code>mscorlib.dll!System.AppDomain.ExecuteAssembly(string assemblyFile, System.Security.Policy.Evidence assemblySecurity, string[] args)
  Microsoft.VisualStudio.HostingProcess.Utilities.dll!Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
  mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
  mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
  mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
  mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
  mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
  </code></pre>
  
  <hr>
  
  <p>The ISSUE and fix:</p>
  
  <p>It turned out to be an issue of dcasdk dll's dependencies not being found. I did contact the third party provider and they confirmed that the dcasdk dll had dependency with two more dlls(provided by them later) which after adding resolved the issue!</p>
  
  <p>Thanks everyone for helping me understand the issue.</p>
  
  <hr>
  
  <p>I am getting FileNotFoundException again when i try to deploy this as a windows service in a machine runs on Windows server R2.</p>
  
  <p>I tried compiling with "Prefer 32" On which resulted in the "BadImageFormatException".</p>
  
  <p>If i compile using x86 platform target i am getting FileNotFoundException, though all the dlls required are present in the service folder.</p>
  
  <p>Can someone help me understand whats going wrong with the deployment. It works in the development system as expected with "Any CPU, Prefer 32bit ON".</p>