title: How to terminate the session from the service side?
body: <p>Suppose you have created a duplex service - for example using NetTcpBinding - and that is configured to create a new session for each new subscription by a new client as the <a href="http://msdn.microsoft.com/en-us/library/ms752254.aspx" rel="nofollow">publish-subscribe pattern</a>: every time a new client connects, a new service instance is created. If a client wants to disconnect, it invokes the <code>Unsubscribe()</code> method is sufficient to end its session.</p>
  
  <p>Suppose also that the service has a method to verify if the client is active - that is, the client must periodically invoke this method: if the service detects that a client is no longer active, it makes sense that the service decides to disconnect from the client. Similarly, the service may decide to terminate all sessions if the user decides to close the application that hosts it.</p>
  
  <ul>
  <li>How to terminate the session from the service side?</li>
  <li>How could the client (that is, the automatically generated proxy) detect that the service wants to disconnect or that is disconnecting? Does it throw an exception?</li>
  </ul>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I read about the <code>ICommunicationObject</code> interface, the <code>CommunicationObject</code> class, the <code>IDisposable</code> interface, etc.., so I tried to use an <code>ICommunicationObject</code> related to the callback:</p>
  
  <pre><code>ICommunicationObject obj = (ICommunicationObject)callback;
  obj.Close();
  </code></pre>
  
  <p>In this way, the session instance in cleanly destroyed (the destructor <code>Dispose()</code> method is called after calling the <code>Close()</code> method) and If the client try to send a request, then a <code>ProtocolException</code> is launched on it:</p>
  
  <blockquote>
    <p>This channel can no longer be used to send messages as the output
    session was auto-closed due to a server-initiated shutdown. Either
    disable auto-close by setting the
    DispatchRuntime.AutomaticInputSessionShutdown to false, or consider
    modifying the shutdown protocol with the remote server.</p>
  </blockquote>
  
  <p>Similarly, using the code above but calling the <code>Abort()</code> method, the service instance is destroyed, and the <code>CommunicationObjectFaultedException</code> exception is thrown on the client: </p>
  
  <blockquote>
    <p>The communication object, System.ServiceModel.Channels.ServiceChannel,
    cannot be used for communication because it is in the Faulted state.</p>
  </blockquote>
  
  <p>The behavior that I got is pretty much what I want, but I'm not sure if this is the exact way to proceed.</p>