title: How to get WCF services to return fault messages with an HTTP 200 response code?
body: <p>We are trying to get our WCF services return to fault messages with an HTTP 200 response code to our Silverlight 4 clients following <a href="http://msdn.microsoft.com/en-us/library/ee844556%28v=vs.95%29.aspx" rel="nofollow">this article on MSDN</a>. After spending most of the day fighting with the configuration, debugging and searching relevant topics on SO, we still haven't got it working.</p>
  
  <p>Here is our SilverlightFaultBehaviour class:</p>
  
  <pre><code>public class SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
  {
      public void ApplyDispatchBehavior(ServiceEndpoint endpoint, 
          EndpointDispatcher endpointDispatcher)
      {
          SilverlightFaultMessageInspector inspector = 
          new SilverlightFaultMessageInspector();
          endpointDispatcher.DispatchRuntime
              .MessageInspectors.Add(inspector);
      }
  
      public class SilverlightFaultMessageInspector : IDispatchMessageInspector
      {
          public void BeforeSendReply(ref Message reply,
              object correlationState)
          {
              if (reply.IsFault)
              {
                  HttpResponseMessageProperty property = 
                      new HttpResponseMessageProperty();
  
                  // Here the response code is changed to 200.
                  property.StatusCode = System.Net.HttpStatusCode.OK;
  
                  reply.Properties[HttpResponseMessageProperty.Name] = property;
              }
          }
  
          public object AfterReceiveRequest(ref Message request,
              IClientChannel channel, 
              InstanceContext instanceContext)
          {
              // Do nothing to the incoming message.
              return null;
          }
      }
  
      // The following methods are stubs and not relevant. 
      public void AddBindingParameters(ServiceEndpoint endpoint,
          BindingParameterCollection bindingParameters)
      {
      }
  
      public void ApplyClientBehavior(ServiceEndpoint endpoint, 
          ClientRuntime clientRuntime)
      {
      }
  
      public void Validate(ServiceEndpoint endpoint)
      {
      }
  
      public override Type BehaviorType
      {
          get { return typeof(SilverlightFaultBehavior); }
      }
  
      protected override object CreateBehavior()
      {
          return new SilverlightFaultBehavior();
      }
  }
  </code></pre>
  
  <p>And here is the relevant snippet from our web.config:</p>
  
  <pre><code>&lt;system.serviceModel&gt;
    &lt;behaviors&gt;
      &lt;serviceBehaviors&gt;
        &lt;behavior name="FooWebServiceBehaviour"&gt;
          &lt;serviceDebug includeExceptionDetailInFaults="true" /&gt;
          &lt;serviceMetadata httpGetEnabled="true" /&gt;
        &lt;/behavior&gt;
      &lt;/serviceBehaviors&gt;
      &lt;endpointBehaviors&gt;
        &lt;behavior name="SilverlightFaultBehavior"&gt;
          &lt;silverlightFaults /&gt;
        &lt;/behavior&gt;
      &lt;/endpointBehaviors&gt;
    &lt;/behaviors&gt;
    &lt;services&gt;
      &lt;service name="Foo.Web.Services.BarService" behaviorConfiguration="FooWebServiceBehaviour"&gt;
        &lt;endpoint address="" binding="customBinding"
          bindingConfiguration="Foo.Web.Services.HttpBinding"
          contract="Foo.Web.Service.IBarService"
          behaviorConfiguration="SilverlightFaultBehavior" /&gt;
      &lt;/service&gt;
    &lt;/services&gt;
    &lt;extensions&gt;
      &lt;behaviorExtensions&gt;
        &lt;add name="silverlightFaults"
          type="Foo.Web.Behavior.SilverlightFaultBehavior, Foo.WebBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
      &lt;/behaviorExtensions&gt;
    &lt;/extensions&gt;
  &lt;/system.serviceModel&gt;
  </code></pre>
  
  <p>The problem seems to be that the BeforeSendReply method is never called, and our clients still receive the useful "Not Found" error every time we throw a FaultException.</p>
  
  <p>Is there something blatantly obvious that we are missing to get the services to return the response code we want?</p>