title: WCF: Accessing the service instance from the server
body: <p><strong>Context:</strong></p>
  
  <p>I need to develop a monitoring server that monitors some of our applications (these applications are in c#). So I decided to develop the system with WCF which seems suitable for my needs. </p>
  
  <p>These applications must register themselves to the monitoring server when they start. After that the monitoring server can call the methods Start or Stop of these applications.</p>
  
  <p>Everything is completely executed on the same machine, nothing needs to be executed remotely.</p>
  
  <p>So I developed a good prototype and everything works fine. Each application registers itself to the monitoring server. </p>
  
  <p><strong>Question:</strong></p>
  
  <p><code>ApplicationRegistrationService</code> (see the code below) is the implementation of the monitoring service and it is a singleton instance due to the <code>ServiceBehavior</code> attribute.</p>
  
  <p>Here my problem: I want to access the content of <code>ApplicationRegistrationService</code> per example, the number of connected applications from my server (<code>ConsoleMonitoringServer</code> in the example). But, I am not sure how to achieve this.</p>
  
  <p>Do I need to create a channel in my server to the service like I did in my clients (<code>ConsoleClient</code>) or it exists a better way to achieve this?</p>
  
  <p><strong>Code:</strong></p>
  
  <p>The code is very simplified for the purpose of this question:</p>
  
  <pre><code>//The callback contract interface
  public interface IApplicationAction
  {
      [OperationContract(IsOneWay = true)]
      void Stop();
  
      [OperationContract(IsOneWay = true)]
      void Start();
  }
  
  [ServiceContract(SessionMode = SessionMode.Required, 
      CallbackContract = typeof(IApplicationAction))]
  public interface IApplicationRegistration
  {
      [OperationContract]
      void Register(Guid guid, string name);
  
      [OperationContract]
      void Unregister(Guid guid);
  }
  
  [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, 
      ConcurrencyMode = ConcurrencyMode.Multiple)]
  public class ApplicationRegistrationService : IApplicationRegistration
  {
      //IApplicationRegistration Implementation
  }
  
  public class ApplicationAction : IApplicationAction
  {
      //IApplicationAction Implementation
  }
  </code></pre>
  
  <p><strong>Console application for this example</strong></p>
  
  <pre><code>class ConsoleClient
  {
      static void Main(string[] args)
      {
          ApplicationAction actions = new ApplicationAction();
  
          DuplexChannelFactory&lt;IApplicationRegistration&gt; appRegPipeFactory =
              new DuplexChannelFactory&lt;IApplicationRegistration&gt;(actions,
              new NetNamedPipeBinding(), new EndpointAddress("net.pipe://localhost/AppReg"));
  
          IApplicationRegistration proxy = appRegPipeFactory.CreateChannel();
          proxy.Register(Guid.Empty, "ThisClientName");
  
          //Do stuffs
      }
  }
  </code></pre>
  
  <p><strong>Console server for this example</strong></p>
  
  <pre><code>class ConsoleMonitoringServer
  {
      static void Main(string[] args)
      {
          using (ServiceHost host = new ServiceHost(typeof(ApplicationRegistrationService),
              new Uri[]{ new Uri("net.pipe://localhost")}))
          {
              host.AddServiceEndpoint(typeof(IApplicationRegistration), 
                  new NetNamedPipeBinding(), "AppReg");
  
              host.Open();
  
              //Wait until some write something in the console
              Console.ReadLine();
  
              host.Close();
          }
      }
  }
  </code></pre>