title: WCF Named Pipe IPC
body: <p>I have been trying to get up to speed on Named Pipes this week.  The task I am trying to solve with them is that I have an existing windows service that is acting as a device driver that funnels data from an external device into a database.  Now I have to modify this service and add an optional user front end (on the same machine, using a form of IPC) that can monitor the data as it passes between the device and the DB as well as send some commands back to the service.</p>
  
  <p>My initial ideas for the IPC were either named pipes or memory mapped files.  So far I have been working through the named pipe idea using <a href="http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication" rel="nofollow noreferrer">WCF Tutorial Basic Interprocess Communication</a> .  My idea is to set the Windows service up with an additional thread that implements the WCF NamedPipe Service and use that as a conduit to the internals of my driver.</p>
  
  <p>I have the sample code working, however I can not get my head around 2 issues that I am hoping that someone here can help me with:</p>
  
  <ol>
  <li><p>In the tutorial the ServiceHost is instantiated with a typeof(StringReverser) rather than by referencing a concrete  class.  Thus there seems to be no mechanism for the Server to interact with the service itself (between the host.Open() and host.Close() lines).  Is it possible to create a link between and pass information between the server and the class that actually implements the service?  If so, how?</p></li>
  <li><p>If I run a single instance of the server and then run multiple instance of the clients, it seems that each client gets a separate instance of the service class.  I tried adding some state information to the class implementing the service and it was only retained within the instance of the named pipe.  This is possibly related to the first question, but is there anyway to force the named pipes to use the same instance of the class that is implementing the service?</p></li>
  <li><p>Finally, any thoughts on MMF vs Named Pipes?</p></li>
  </ol>
  
  <p><strong>Edit - About the solution</strong></p>
  
  <p>As per Tomasr's answer the solution lies in using the correct constructor in order to supply a concrete singleton class that implements the service (<a href="http://msdn.microsoft.com/en-us/library/ms585487.aspx" rel="nofollow noreferrer">ServiceHost Constructor (Object, Uri[])</a>).  What I did not appreciate at the time was his reference to ensuring the service class was thread safe.  Naively just changing the constructor caused a crash in the server, and that ultimately lead me down the path of understanding InstanceContextMode from this blog entry <a href="http://www.wintellect.com/cs/blogs/jsmith/archive/2006/05/16/instancecontextmode-and-concurrencymode.aspx" rel="nofollow noreferrer">Instancecontextmode And Concurrencymode</a>. Setting the correct context nicely finished off the solution.</p>