title: WCF: FaultContract set but not causing FaultException<T> to be caught in REST service
body: <p>I'm just starting with WCF and am trying to verify fault handling in a WCF rest service that supports both JSON and XML. <a href="https://gist.github.com/3383673#file_server_program.cs" rel="nofollow">My test service</a> generates a fault but no matter what I try I can't get <a href="https://gist.github.com/3383673#file_client_program.cs" rel="nofollow">my client</a> to pick up the details of the fault (and the behavior varies based on the request format and http status code):</p>
  
  <p>My test service generates the fault as follows:</p>
  
  <pre><code>public Data GetResponse()
  {
      throw new WebFaultException&lt;ErrorDetails&gt;(
          new ErrorDetails {ErrorMessage = "Server Config Value not set"},
          HttpStatusCode.OK
          );
  }
  </code></pre>
  
  <p>This gets sent across the wire quite reasonably:</p>
  
  <pre><code>{"ErrorMessage":"Server Config Value not set"}
  </code></pre>
  
  <p>And:</p>
  
  <pre><code>&lt;ErrorDetails xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
      &lt;ErrorMessage&gt;Server Config Value not set&lt;/ErrorMessage&gt;
  &lt;/ErrorDetails&gt;
  </code></pre>
  
  <p>My client is defined with a <code>FaultContract</code>:</p>
  
  <pre><code>[OperationContract]
  [WebInvoke(
      UriTemplate="/response",
      Method="GET",
      RequestFormat = WebMessageFormat.Xml, // or .Json
      ResponseFormat = WebMessageFormat.Xml // or .Json
      )]
  [FaultContract(typeof(ErrorDetails), Namespace="")]
  Data GetResponse();
  </code></pre>
  
  <p>Here's the full error message for (format/status code):</p>
  
  <p>XML/Conflict:</p>
  
  <blockquote>
    <p>Requesting response
    CommunicationException: The remote server returned an unexpected response: (409) Conflict., System.Collections.ListDictionary
    Internal, System.ServiceModel.ProtocolException
    Press a key to exit...</p>
  </blockquote>
  
  <p>And XML/OK:</p>
  
  <blockquote>
    <p>Requesting response
    Exception: Unable to deserialize XML body with root name 'ErrorDetails' and root namespace '' (for operation 'GetResponse' an
    d contract ('IClient',  '')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the know
    n types collection of the service., System.Collections.ListDictionaryInternal
    Press a key to exit...</p>
  </blockquote>
  
  <p>And JSON/Conflict:</p>
  
  <blockquote>
    <p>Requesting response
    CommunicationException: The remote server returned an unexpected response: (409) Conflict., System.Collections.ListDictionary
    Internal, System.ServiceModel.ProtocolException
    Press a key to exit...</p>
  </blockquote>
  
  <p>And JSON/OK:</p>
  
  <blockquote>
    <p>Requesting response
    Response:
    Request complete
    Press a key to exit...</p>
  </blockquote>
  
  <p>The client code catches the exceptions in the proper order:</p>
  
  <pre><code>try
  {
      Console.WriteLine("Requesting response");
      Console.WriteLine("Response: " + client.GetResponse().Message);
      Console.WriteLine("Request complete");
  }
  // sanity check, just in case...
  catch (WebFaultException&lt;ErrorDetails&gt; ex)
  {
      Console.WriteLine("WebFaultException&lt;ErrorDetails&gt;: " + ex.Detail.ErrorMessage + ", " + ex.Reason);
  }
  catch (FaultException&lt;ErrorDetails&gt; ex)
  {
      Console.WriteLine("FaultException&lt;ErrorDetails&gt;: " + ex.Detail.ErrorMessage + ", " + ex.Reason);
  }
  catch (FaultException ex)
  {
      Console.WriteLine("FaultException: " + ex.Message + ", " + ex.Reason);
  }
  catch (CommunicationException ex)
  {
      Console.WriteLine("CommunicationException: " + ex.Message + ", " + ex.Data + ", " + ex.GetType().FullName);
  }
  catch (Exception ex)
  {
      Console.WriteLine("Exception: " + ex.Message + ", " + ex.Data);
  }
  </code></pre>
  
  <p>What do I have to do so that <code>FaultException&lt;ErrorDetails&gt;</code> will be thrown and I can get access to the <code>ErrorDetails</code>?</p>
  
  <p>Note: The <a href="https://gist.github.com/3383673" rel="nofollow">gist</a> should be fully compileable and runnable.</p>