title: Where should I cleanup private resources for a ServiceHost?
body: <p>I've set up a WCF NamedPipes Server/Client pair of applications but when the Host stops and calls <code>ServiceHost.Close()</code> it doesn't seem to appropriately cleanup the object from which the Host was created (using an object rather than a type to instantiate the host).</p>
  
  <p>The server application code is as follows:</p>
  
  <pre><code>static void Main(string[] args) {
    // Specify the device type. Consider command line arguments here.
    DeviceTypes deviceType = DeviceTypes.myDeviceType;
  
    // Create the service object.
    ServiceHost selfHost = ServiceFactory.CreateService(deviceType);
    try {
      // Start the service.
      selfHost.Open();
      System.Console.WriteLine("The service is ready.");
      System.Console.WriteLine("Press &lt;ENTER&gt; to terminate service.");
      System.Console.WriteLine();
      System.Console.ReadLine();
  
      // Stop the service.
      System.Console.WriteLine("Closing the service.");
      selfHost.Close();
    } catch (CommunicationException e) {
      System.Console.WriteLine("An exception occurred: {0}", e.Message);
      selfHost.Abort();
    }
  }
  </code></pre>
  
  <p>The <code>ServiceFactory.CreateService</code> method:</p>
  
  <pre><code>public static ServiceHost CreateService(DeviceTypes type) {
    // Create the client object.
    // Create_Server only creates an uninitialized child class based on the type.
    IMyServer client = MyServerFactory.Create_Server(type);
  
    // Initialize the client.
    client.Initialize();
  
    // Create the service object.
    MyService server = new MyService(client);
  
    // Create a service host.
    ServiceHost selfHost = new ServiceHost(server, new System.Uri(baseAddress));
  
    // Add a service endpoint.
    selfHost.AddServiceEndpoint(typeof(IMyActions), new NetNamedPipeBinding(), relAddress);
    return selfHost;
  }
  </code></pre>
  
  <p>The <code>MyService</code> class (which contains the resource I want to clean up):</p>
  
  <pre><code>[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = 
  InstanceContextMode.Single)]
  class MyService : IMyActions
  {
      private IMyServer server;
  
      public MyService(IMyServer server) {
          this.server = server;
      }
  
      // Bunch of public operations to carry out the contract
      public bool Initialize() {
          return server.Initialize();
      }
  
      public void Dispose() {
          server.Dispose();
      }
  
      // No-op to check pipe connection.
      public void ConfirmConnection() { }
  }
  </code></pre>
  
  <p>How and where should I call the <code>server.Dispose()</code> method in the <code>MyService</code> class?
  I thought that this would be handled by <code>selfHost.Close()</code> but it seems not...</p>