title: BadImageFormatException encountered with WcfSvcHost and IIS WCF host
body: <p>Creating a WCF Service Library in Visual Studio 2008 on Vista x64 is troublesome when referencing an x86 DLL. A service that calls a 32-bit DLL is required to have a platform target of x86 to run on a 64-bit OS. When you do this, the WcfSvcHost throws a BadImageFormatException when you attempt to debug the service. There is a <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=349510" rel="nofollow noreferrer">bug report</a> on MS connect. The workaround I used was to <a href="https://connect.microsoft.com/VisualStudio/feedback/Workaround.aspx?FeedbackID=349510" rel="nofollow noreferrer">coreflag WcfSvcHost as 32-bit</a>.</p>
  
  <p><strong>Manifest Problem</strong></p>
  
  <p>The main issue I've run in to is this third-party native 32-bit DLL fails to load using certain WCF hosts. I receive the following error when <strong>a service operation is invoked</strong> that uses the third-party DLL:</p>
  
  <blockquote>
    <p>System.TypeInitializationException: The type initializer for
    '' threw an exception.</p>
    
    <p>.ModuleLoadExceptionHandlerException: 
    A nested exception occurred after the
    primary exception that caused the C++
    module to fail to load.</p>
    
    <p>System.BadImageFormatException: The module was expected to contain an
    assembly manifest. (Exception from
    HRESULT: 0x80131018)</p>
  </blockquote>
  
  <p>NestedException:</p>
  
  <blockquote>
    <p>The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))</p>
  </blockquote>
  
  <p>This exception is not raised when WcfSvcHost starts, it's raised when the a service operation is invoked that references the 32-bit DLL. What's very interesting, hosting this same service with the same app.config on a console app has no exceptions and works perfectly:</p>
  
  <pre><code>using (ServiceHost host = new ServiceHost (typeof (MsgBrokerService))) {
      host.Open ();
      Console.WriteLine ("running");
      Console.ReadLine ();
  </code></pre>
  
  <p>This exception occurs right after:</p>
  
  <blockquote>
    <p>'WcfSvcHost.exe' (Managed): Loaded
    'C:\Windows\WinSxS\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.3053_
    none_d08d7bba442a9b36\msvcm80.dll'</p>
  </blockquote>
  
  <p>Again, the console app does not have an exception and loads the same DLL:</p>
  
  <blockquote>
    <p>'ConsoleApp.vshost.exe' (Managed):
    Loaded
    'C:\Windows\WinSxS\x86_microsoft.vc80.crt_1fc8b3b9a1e18e3b_8.0.50727.3053_
    none_d08d7bba442a9b36\msvcm80.dll'</p>
  </blockquote>
  
  <p><strong>See answer</strong> <a href="https://stackoverflow.com/questions/727313/wcf-problems-with-hosting-on-wcfsvchost-and-iis/942578#942578">from Microsoft Product Support</a>.</p>
  
  <p>Update #1: Both the console application and the WcfSvcHost.exe host process runs under the same session and logged-in user (me). I've copied WcfSvcHost.exe to the directory of the service, manually launched and experienced the same result. I've also checked the Windows Event Log for additional information and used <em>sxstrace</em>, but nothing was logged.</p>
  
  <p>Running Process Explorer, I've verified the following are the same between the two processes:</p>
  
  <ul>
  <li>Image: 32-bit</li>
  <li>Current Directory</li>
  <li>User/SID</li>
  <li>Session</li>
  <li>Security (groups denied, privileges disabled)</li>
  </ul>
  
  <p>Running Process Monitor, and <a href="http://blogs.msdn.com/vijaysk/archive/2009/04/02/getting-better-stack-traces-in-process-monitor-process-explorer.aspx" rel="nofollow noreferrer">configuring symbols</a>, I see WcfSvcHost looks for the following registry and files, while the console host does not. <em>Process Monitor logs a lot of data and I'm not sure what I'm looking for :(</em>.</p>
  
  <blockquote>
    <p>HKLM\SOFTWARE\Microsoft\Fusion\PublisherPolicy\Default\policy.8.0.msvcm80__b03f5f7f11d50a3a
    C:\Windows\assembly\GAC_32\msvcm80\8.0.50727.3053__b03f5f7f11d50a3a
    C:\Windows\assembly\GAC_MSIL\msvcm80\8.0.50727.3053__b03f5f7f11d50a3a
    C:\Windows\assembly\GAC\msvcm80\8.0.50727.3053__b03f5f7f11d50a3a</p>
  </blockquote>
  
  <p>Update #2: This same exception occurs when the service is <strong>hosted in production</strong> on IIS 6 / Windows Server 2003.</p>
  
  <p>Update #3: The 3rd-party 32-bit .NET assembly is the <a href="http://streambase.com/developers/docs/latest/apiguide/creatingdotnetclients.html" rel="nofollow noreferrer">StreamBase API</a>:</p>
  
  <ul>
  <li>sbclient.dll (managed)</li>
  <li>monitor.netmodule (managed)</li>
  <li>dotnetapi.dll (unmanaged)</li>
  <li>pthreads-vc8.dll (unmanaged)</li>
  </ul>
  
  <p>Update #4: Added manifests without success:</p>
  
  <ol>
  <li>Verified that dotnetapi.dll and pthreads-vc8.dll have RT_MANIFEST. The sbclient.dll .NET assembly did not have a manifest</li>
  <li>Removed sbclient.dll from the GAC</li>
  <li>Registered sbclient.dll for verification skipping</li>
  <li>Added a manifest via <a href="http://blogs.msdn.com/cheller/archive/2006/08/24/718757.aspx" rel="nofollow noreferrer">mt.exe</a> to both sbclient.dll and monitor.netmodule</li>
  <li>Verified manifest was added and that the expected files were loaded during testing (via Visual Studio - debug modules window)</li>
  <li>The same BadImageFormatException is thrown under BackgroundWorker.OnDoWork(), and the call stack shows a call to dotnetapi.dll...DefaultDomain.Initalize().</li>
  </ol>
  
  <p>I have verified that msvcm80.dll does not have a manifest, I believe this is the only file loaded that doesn't have a manifest :)</p>
  
  <p>Interesting find</p>
  
  <p>When I load monitor.netmodule in <a href="http://www.red-gate.com/products/reflector/" rel="nofollow noreferrer">Reflector</a>, it says:</p>
  
  <blockquote>
    <p>'monitor.netmodule' does not contain
    an assembly manifest.</p>
  </blockquote>
  
  <p>Even though it displays an error, Reflector is still able to disassemble the managed code.</p>