title: Exposing a winforms application internals through WCF
body: <p>I am trying to take my first steps into IPC and the WCF, and so far, I'm falling flat on my face. I have a Winforms application that I would like to instrument somewhat for remote callers. The winforms application has most of its business logic confined to a singleton that takes care of all the background work. I would like to expose some of the functionality through an IPC mechanism. WCF seems like the way forward, so I started out with that.</p>
  
  <p>What I tried is adding a WCF Service Library project to my solution, through which I would like to expose some calls. When I start the Winforms project in the VS debugger, it runs as normal, and the WcfSvcHost starts up. I can communicate to the WCF service with the WcfTestClient.</p>
  
  <p>However, when I try to access the singleton holding the code I would like to communicate with, it seems like I am getting a new singleton object. Clearly, I'm doing it wrong; what I guess is happening is that the service runs in a different process, so there is no real shared code, and hence no shared singleton.</p>
  
  <p>I'm not sure how I should continue. Is my choice of using WCF for IPC the wrong one? Should I integrate the WCF endpoints in the Winforms application? Is what I'm trying even feasible?</p>
  
  <p>EDIT: I figured this was so high-level, and also so simple that any code sample would be useless. I think I was wrong. So some code:</p>
  
  <p>In the WinForms assembly:</p>
  
  <pre><code>public partial class Form1 : Form
  {
    public Form1()
      {
        InitializeComponent();
        label1.Text = MySingleton.Instance.InitedAt.ToString();
      }
  }
  
  public class MySingleton
  {
    private static MySingleton instance = new MySingleton();
    private DateTime inited;
  
    private MySingleton()
    {
      this.inited = DateTime.Now;
    }
  
    public static MySingleton Instance
    {
      get
      {
        return instance;
      }
    }
  
    public DateTime InitedAt
    {
      get
      {
        return this.inited;
      }
    }
  }
  </code></pre>
  
  <p>In the WCFServiceLibrary assembly:</p>
  
  <pre><code>[ServiceContract]
  public interface IApplicationProbe {
  
    [OperationContract]
    string DoesItWork();
  
    [OperationContract]
    string SingletonInited();
  }
  
  public class ApplicationProbe : IApplicationProbe {
    public string DoesItWork(){
      return "Why yes, yes it does";
    }
  
    public string SingletonInited(){
      return MySingleton.Instance.InitedAt.ToString();
    }
  }
  </code></pre>
  
  <p>when I query <code>SingletonInited</code> through the WcfTestClient, I get an InitedAt which is not the same DateTime as the instatiation of the winforms singleton.</p>
  
  <p>Edit2:</p>
  
  <p>I have this code running as is (with the auto-generated scaffolding around the Winforms stuff). The label on the form displays a different time than the time returned from the WCF call, demonstrating it is a different instance.    </p>