title: Throwing FaultException using Custom Exception Handler EL WCF
body: <p>So I'm trying to use Enterprise Library in my WCF service to do some of the exception-related work for me. </p>
  
  <p>My idea is to set up a "<strong>Custom Exception Handler</strong>" for say "NullReferenceException" and in the "Custom Exception Handler" create FaultException exception. </p>
  
  <p>My understanding is that this "new" exception will then make it across the wire and I will catch it at the client. </p>
  
  <p>Some code for better understanding:</p>
  
  <p>WCF service:</p>
  
  <pre><code>[ServiceContract(Name="MyService", ConfigurationName="MyNamespace.MyService")]
      [ExceptionShielding("PolicyName")]
      public interface IMyService
      {
          [OperationContract]
          [FaultContract(typeof(MyFaultContract))]
          string Method(String Param);
  }
  
  public class MyService : IMyService
      {
          public string Method(String Param)
          {
            throw new  NullReferenceException("code-created null message here");
          }
  }
  </code></pre>
  
  <p>Custom Exception Handler: (Enterprise library)</p>
  
  <pre><code>public class MyExceptionHandler : IExceptionHandler
      {
          public MyExceptionHandler(NameValueCollection collection) { }
  
          public MyExceptionHandler() { }
  
          public System.Exception HandleException(System.Exception exception, 
                                Guid handlingInstanceId)
          {
                  MyFaultContract details = new MyFaultContract();
                  if (exception is NullReferenceException)
                  {
                      details.ErrorCode = MyFaultCode.NullReferenceException;
                      details.OriginalMessage = exception.Message;
                      details.MyMessage = "Null Reference exception here!";
                  }
                  return new FaultException&lt;MyFaultContract&gt;(details);
              }
  }
  </code></pre>
  
  <p>App Config File that maps NullReferenceException to "Custom Exception Handler":</p>
  
  <pre><code>&lt;exceptionPolicies&gt;
        &lt;add name="PolicyName"&gt;
          &lt;exceptionTypes&gt;
            &lt;add name="NullReferenceException" type="System.NullReferenceException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
              postHandlingAction="ThrowNewException"&gt;
              &lt;exceptionHandlers&gt;
                &lt;add type="MyExceptionHandler, MyApplication, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"
                  name="MyExceptionHandler" /&gt;
              &lt;/exceptionHandlers&gt;
            &lt;/add&gt;
          &lt;/exceptionTypes&gt;
        &lt;/add&gt;
      &lt;/exceptionPolicies&gt;
  </code></pre>
  
  <p>And finally Client code that expects to catch this FaultException:</p>
  
  <pre><code>MyService.MyServiceClient client = new MyService.MyServiceClient();
              client.Open();
              try
              {
                  string result = client.Method(string parameter);
              }
  
              catch (System.ServiceModel.FaultException&lt;MyService.MyFaultContract&gt; ex)
              {
                  // where I think exception should end up
              }
              catch (System.ServiceModel.FaultException ex)
              {
                  // general FaultException
              }
  </code></pre>
  
  <p>But instead I get a ProtocolException, stating that reply action is required:</p>
  
  <blockquote>
    <p>A reply message was received for operation 'Method' with action ''.
    However, your client code requires action
    'http://tempuri.org/MyService/MethodResponse'.</p>
  </blockquote>
  
  <p>What am I doing wrong? Is it possible to return (not explicitly throw) an FaultException with custom FaultContract in the "Custom Exception Handler"?</p>
  
  <p>Any advice is appreciated.</p>
  
  <p><strong>Update:</strong>
  As you can see, post-handling action in the "Custom Exception Handler" is set to "ThrowNewException". 
  If I change it to "NotifyRethrow" I don't het "ProtocolException" any more!
  Instead, client catches a regular "FaultException" (not custom-typed).</p>
  
  <p>Now the question is why original custom-typed FaultException doesn't make it across the wire.</p>
  
  <p><strong>Update 2</strong>
  One thing I forgot to mention is the fact that my WCF service is running within the ServiceHost, not under IIS. So basically I have a Windows service that creates a ServiceHost and exposes Service1 interface through this ServiceHost.</p>
  
  <p>The reason why I think this may have something to do with this problem is that Enterprise Library claims it deals with exceptions throughout entire application, not only within service boundaries. Maybe this cause the exception to be thrown too late? Or not at the correct level?</p>
  
  <p><strong>Update 3</strong><br>
  Thanks for the post!
  You're right, the only reason why I'm messing with custom handler is because I want to set MyFaultCode value. 
  I tried your advice - having 2 handlers configured.</p>
  
  <p>First is a custom handler and catches NullReference exception. It than throws a new exception - MyApplicationException, with MyFaultContract field. </p>
  
  <p>Then I configured second handler - built-in "Fault Contract Exception Handler" that catches MyApplicationException, creates new FaultException and automatically maps MyFaultcontract from MyApplicationException to the newly created FaultException.</p>
  
  <p>WCF client still catches generic FaultException, not the custom-contract one.  </p>