title: WCF Duplex: How to handle thrown exception in duplex Callback
body: <p>How do I handle an exception thrown in a callback method on the client in a WCF duplex setup? </p>
  
  <p>Currently, the client does not appear to raise the faulted event (unless I'm monitoring it incorrectly?) but any subsequent to call Ping() using the the client fails with CommunicationException: "The communication object, System.ServiceModel.Channels.ServiceChannel, cannot be used for communication because it has been Aborted.".</p>
  
  <p>How do I deal with this and recreate the client etc? My first question is how to find out when it happens. Secondly, how best to deal with it?</p>
  
  <p>My service and callback contracts:</p>
  
  <pre><code>[ServiceContract(CallbackContract = typeof(ICallback), SessionMode = SessionMode.Required)]
  public interface IService
  {
      [OperationContract]
      bool Ping();
  }
  
  public interface ICallback
  {
      [OperationContract(IsOneWay = true)]
      void Pong();
  }
  </code></pre>
  
  <p>My server implementation:</p>
  
  <pre><code>[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, ConcurrencyMode = ConcurrencyMode.Single)]
  public class Service : IService
  {
      public bool Ping()
      {
          var remoteMachine = OperationContext.Current.GetCallbackChannel&lt;ICallback&gt;();
  
          remoteMachine.Pong();
      }
  }
  </code></pre>
  
  <p>My client implementation:</p>
  
  <pre><code>[CallbackBehavior(UseSynchronizationContext = false, ConcurrencyMode = ConcurrencyMode.Single)]
  public class Client : ICallback
  {
      public Client ()
      {
          var context = new InstanceContext(this);
          var proxy = new WcfDuplexProxy&lt;IApplicationService&gt;(context);
  
          (proxy as ICommunicationObject).Faulted += new EventHandler(proxy_Faulted);
  
          //First Ping will call the Pong callback. The exception is thrown
          proxy.ServiceChannel.Ping();
          //Second Ping call fails as the client is in Aborted state
          try
          {
              proxy.ServiceChannel.Ping();
          }
          catch (Exception)
          {
              //CommunicationException here 
              throw;
          }
      }
      void Pong()
      {
          throw new Exception();
      }
  
      //These event handlers never get called
      void proxy_Faulted(object sender, EventArgs e)
      {
          Console.WriteLine("client faulted proxy_Faulted");
      }
  }
  </code></pre>