title: WCF call that references Windows Service
body: <p>I have a long-running service that gets data from one source, manipulates it, stores it in a database, etc.</p>
  
  <p>I'd like to expose some methods on that service to other applications. Currently we do this via .NET Remoting but I'd like to move to WCF.</p>
  
  <p>Unfortunately, I the endpoint I connect to is never the one I exposed via my long-running service. Below is a simple example:</p>
  
  <pre><code>    [ServiceContract]
      public interface ITestWcfService
      {
          [OperationContract]
          CounterResult GetCurrentValue();
      }
  
  public class TestWcfService : ITestWcfService
      {
          private ITestWindowsService _service;
          public TestWcfService() { /*NOTE: For discoverability*/ }
          public TestWcfService(ITestWindowsService service)
          {
              _service = service;
          }
  
          public CounterResult GetCurrentValue()
          {
              return _service.GetCurrentValue();
          }
      }
  
  public interface ITestWindowsService
      {
          CounterResult GetCurrentValue();
      }
  </code></pre>
  
  <p>Then I have my actual Windows Service, which self-hosts the WCF service via the ServiceHost class.</p>
  
  <pre><code>public partial class TestWindowsService : ServiceBase, ITestWindowsService
  {
      private static ServiceHost _wcfService;
  
      public TestWindowsService()
      {
          InitializeComponent();
      }
  
      public void OnStart(string[] args)
      {
          //Create instance of WCF, passing in reference to this service instance
          TestWcfService wcf = new TestWcfService(this);
          _wcfService = new ServiceHost(wcf);
      }
  
          public CounterResult GetCurrentValue()
      {
          //Note: Some logic here
      }
  }
  </code></pre>
  
  <p>Now, this more-or-less works except that each time I make a call to the <code>TestWcfServiceClient()</code>, it uses the default constructor and creates a new instance of the Wcf Service and does not use the instance created by the windows service. This means that when I call <code>GetCurrentValue()</code> I get a null reference because the <code>_service</code> member hasn't been set.</p>
  
  <p>I've looked around for solutions and found some citing <code>ServiceHostFactory</code> and <code>ServiceHost</code> and <code>IInstanceProvider</code> but each has seemed to be very, very complicated. </p>
  
  <p>Any thoughts you could provide would be most appreciated.</p>
  
  <p><strong>EDIT</strong>: Here's my ServiceModel info</p>
  
  <pre><code>&lt;system.serviceModel&gt;
      &lt;services&gt;
        &lt;service name="WcfService.TestWcfService"&gt;
          &lt;host&gt;
            &lt;baseAddresses&gt;
              &lt;add baseAddress = "http://localhost:8733/Design_Time_Addresses/WcfService/TestWcfService/" /&gt;
            &lt;/baseAddresses&gt;
          &lt;/host&gt;
          &lt;!-- Service Endpoints --&gt;
          &lt;!-- Unless fully qualified, address is relative to base address supplied above --&gt;
          &lt;endpoint address="" binding="basicHttpBinding" contract="WcfService.ITestWcfService"&gt;
            &lt;!-- 
                Upon deployment, the following identity element should be removed or replaced to reflect the 
                identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
                automatically.
            --&gt;
            &lt;identity&gt;
              &lt;dns value="localhost"/&gt;
            &lt;/identity&gt;
          &lt;/endpoint&gt;
          &lt;!-- Metadata Endpoints --&gt;
          &lt;!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --&gt; 
          &lt;!-- This endpoint does not use a secure binding and should be secured or removed before deployment --&gt;
          &lt;endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/&gt;
        &lt;/service&gt;
      &lt;/services&gt;
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;!-- To avoid disclosing metadata information, 
            set the values below to false before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="True" httpsGetEnabled="True"/&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, 
            set the value below to true.  Set to false before deployment 
            to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="False" /&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
    &lt;/system.serviceModel&gt;
  </code></pre>