title: How do I get the cause of a WCF Client Faulted Event?
body: <p>How do you get an exception or error code that caused a WCF Client's Faulted event?</p>
  
  <pre><code>private void ConnectionFaultedHandler(object sender, EventArgs e) 
  {
      // Darn! Something went wrong.
      // Better clean up.
      this._Connection.Abort();
      this._Connection.InnerChannel.Faulted -= this.ConnectionFaultedHandler;
  
      // I'd really like to tell the user some detail of what blew up.
      // But there is no Exception (or similar) property on the event.
      this.NotifyUIOfConnectionFailure(e.Exception);  
  }
  </code></pre>
  
  <p>Note, this is similar to <a href="https://stackoverflow.com/questions/381345/in-wcf-in-the-faulted-event-how-do-i-get-the-exception-details">this thread</a>, except I 1) couldn't get that way to work and 2) it seems to be addressing the issue on the service side, I'd like to deal with in the client.</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>To clarify, the above handler is part of a connection that stays open for a long time (hours or even days); it has a callback interface to receive data from the service. I am not asking about exceptions when you call methods like Open or methods that are part of the contract interface, but a fault that occurs because (for example) someone removed a network cable from your PC, or your Internet connection just failed.</p>
  
  <p>Imagine it occurs some time after this code executes:</p>
  
  <pre><code>private void OpenConnection()
  {
      try
      {
           this._Connection.Open();
      } 
      catch (Exception ex)
      {
           // Yes, I should be catching CommunicationsException, 
           // and TimeoutException, but space is short on StackOverflow.
           return;
      }
  
      Debug.Assert(this._Connection.State == Open);
  
      this._Connection.InnerChannel.Faulted += this.ConnectionFaultedHandler;
  }
  </code></pre>