title: WCF throws FileNotFound exception for "System.ServiceModel" when creating ServiceHost
body: <p>I'm running into a really strange problem with WCF.  I have 2 projects in a solution, each of them starts up some self-hosted WCF services.</p>
  
  <p>Project 1:</p>
  
  <pre><code>myService = new ServiceHost(typeof(MyService1));
  </code></pre>
  
  <p>Which runs fine. However, when I try to run my 2nd project, which contains:</p>
  
  <pre><code>meService = new ServiceHost(typeof(MyOtherService));
  </code></pre>
  
  <p>I get the exception:</p>
  
  <pre><code>FileNotFoundException
  Could not load file or assembly 'System.ServiceModel' or one of its dependencies.
  The system cannot find the file specified.":"System.ServiceModel
  </code></pre>
  
  <p>Fusion Log:</p>
  
  <pre><code>=== Pre-bind state information ===
  LOG: User = removed\removed
  LOG: DisplayName = System.ServiceModel
   (Partial)
  LOG: Appbase = file:///C:/Work/MySln/MyProj/bin/Dev
  LOG: Initial PrivatePath = NULL
  Calling assembly : System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\Work\MySln\MyProj\bin\Dev\MyProj.dll.temp.config
  LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\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:/Work/MySln/MyProj/bin/Dev/System.ServiceModel.DLL.
  LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel/System.ServiceModel.DLL.
  LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel.EXE.
  LOG: Attempting download of new URL file:///C:/Work/MySln/MyProj/bin/Dev/System.ServiceModel/System.ServiceModel.EXE.
  </code></pre>
  
  <p>The really odd part is that in the debugger, at the time that I get this exception, I can go into 'QuickWatch' in Visual Studio and enter:</p>
  
  <pre><code>new MyOtherService()  // returns new instance properly
  new ServiceHost()     // returns new instance properly
  new ServiceHost(typeof(MyOtherService))  // throws exception, same as above.
  </code></pre>
  
  <p>I tried running SysInternals File System Watcher while debugging, but it shows no 'FileNotFound' errors other than devenv.exe trying to track down .pdb files to generate the stack trace.</p>
  
  <p>Anyone have any other ideas of what to look at?</p>
  
  <hr>
  
  <p><strong>Update #1</strong></p>
  
  <p>I checked out the fusion log for the process, and it seems something strange happens. There are 2 log entries for System.ServiceModel, back to back:</p>
  
  <pre><code>*** Assembly Binder Log Entry  (6/1/2009 @ 10:26:48 AM) ***
  
  The operation was successful.
  Bind result: hr = 0x0. The operation completed successfully.
  
  Assembly manager loaded from:  c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
  Running under executable  C:\Program Files\TestDriven.NET 2.0\ProcessInvocation.exe
  --- A detailed error log follows. 
  
  === Pre-bind state information ===
  LOG: User = removed\removed
  LOG: DisplayName = System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
   (Fully-specified)
  LOG: Appbase = file:///C:/Work/MySln/PFWebIntgTests/bin/Dev
  LOG: Initial PrivatePath = NULL
  LOG: Dynamic Base = NULL
  LOG: Cache Base = C:\Documents and Settings\removed\Local Settings\Temp\TestDrivenShadowCopy\633794488082894732
  LOG: AppName = domain-nunit.addin.dll
  Calling assembly : PFWebIntgTests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\Work\MySln\PFWebIntgTests\bin\Dev\PFWebIntgTests.dll.temp.config
  LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\config\machine.config.
  LOG: Post-policy reference: System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
  LOG: Found assembly by looking in the GAC.
  LOG: Binding succeeds. Returns assembly from C:\WINDOWS\assembly\GAC_MSIL\System.ServiceModel\3.0.0.0__b77a5c561934e089\System.ServiceModel.dll.
  LOG: Assembly is loaded in default load context.
  </code></pre>
  
  <p>Then a 2nd entry:</p>
  
  <pre><code>*** Assembly Binder Log Entry  (6/1/2009 @ 10:26:52 AM) ***
  
  The operation failed.
  Bind result: hr = 0x80070002. The system cannot find the file specified.
  
  Assembly manager loaded from:  c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\mscorwks.dll
  Running under executable  C:\Program Files\TestDriven.NET 2.0\ProcessInvocation.exe
  --- A detailed error log follows. 
  
  === Pre-bind state information ===
  LOG: User = removed\removed
  LOG: DisplayName = System.ServiceModel
   (Partial)
  LOG: Appbase = file:///C:/Work/MySln/PFWebIntgTests/bin/Dev
  LOG: Initial PrivatePath = NULL
  LOG: Dynamic Base = NULL
  LOG: Cache Base = C:\Documents and Settings\removed\Local Settings\Temp\TestDrivenShadowCopy\633794488082894732
  LOG: AppName = domain-nunit.addin.dll
  Calling assembly : System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089.
  ===
  LOG: This bind starts in default load context.
  LOG: Using application configuration file: C:\Work\MySln\PFWebIntgTests\bin\Dev\PFWebIntgTests.dll.temp.config
  LOG: Using machine configuration file from c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\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:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel.DLL.
  LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel/System.ServiceModel.DLL.
  LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel.EXE.
  LOG: Attempting download of new URL file:///C:/Work/MySln/PFWebIntgTests/bin/Dev/System.ServiceModel/System.ServiceModel.EXE.
  LOG: All probing URLs attempted and failed.
  </code></pre>
  
  <p>So it seems that it tries to load System.ServiceModel twice, and once from the local app dir instead of the GAC? I'm confused...</p>