title: WCF exception IErrorHandler not getting called
body: <p>I seem to be having a problem for getting <code>IErrorHandler</code> interface to work. My code is</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Diagnostics;
  using System.ServiceModel;
  using System.ServiceModel.Channels;
  using System.ServiceModel.Configuration;
  using System.ServiceModel.Description;
  using System.ServiceModel.Dispatcher;
  
  namespace WcfService3
  {
      public class Service1 : IService1
      {
          public string GetData(int value)
          {
              throw new Exception("asdf");
  
          }
      }
  
      public class MyErrorHandler : IErrorHandler
      {
          public MyErrorHandler()
          {
              string Hello = "";
          }
          public bool HandleError(Exception error)
          {
              return true;
          }
  
          public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
          {
              var vfc = new MyFault();
              var fe = new FaultException&lt;MyFault&gt;(vfc);
              var fault = fe.CreateMessageFault();
              msg = Message.CreateMessage(version, fault, "http://ns");
          }
      }
  
      public class ErrorHandlerExtension : BehaviorExtensionElement, IServiceBehavior
      {
          public override Type BehaviorType
          {
              get { return GetType(); }
          }
  
          protected override object CreateBehavior()
          {
              return this;
          }
  
          private IErrorHandler GetInstance()
          {
              return new MyErrorHandler();
          }
  
          void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection&lt;ServiceEndpoint&gt; endpoints, BindingParameterCollection bindingParameters)
          {
          }
  
          void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
          {
              IErrorHandler errorHandlerInstance = GetInstance();
              foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
              {
                  dispatcher.ErrorHandlers.Add(errorHandlerInstance);
              }
          }
  
          void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
          {
              foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
              {
                  if (endpoint.Contract.Name.Equals("IMetadataExchange") &amp;&amp;
                      endpoint.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
                      continue;
  
                  foreach (OperationDescription description in endpoint.Contract.Operations)
                  {
                      if (description.Faults.Count == 0)
                      {
                          throw new InvalidOperationException("FaultContractAttribute not found on this method");
                      }
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>My web.config is:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;configuration&gt;
  
    &lt;appSettings&gt;
      &lt;add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" /&gt;
    &lt;/appSettings&gt;
    &lt;system.web&gt;
      &lt;compilation debug="true" targetFramework="4.5" /&gt;
      &lt;httpRuntime targetFramework="4.5"/&gt;
    &lt;/system.web&gt;
    &lt;system.serviceModel&gt;
        &lt;services&gt;
      &lt;service name="WcfService3.Service1"&gt;
        &lt;endpoint address=""
                  binding="basicHttpBinding"
                  contract="WcfService3.IService1" /&gt;
      &lt;/service&gt;
    &lt;/services&gt;
  
      &lt;behaviors&gt;
        &lt;serviceBehaviors&gt;
          &lt;behavior&gt;
            &lt;!-- To avoid disclosing metadata information, set the values below to false before deployment --&gt;
            &lt;serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/&gt;
            &lt;!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information --&gt;
            &lt;serviceDebug includeExceptionDetailInFaults="false"/&gt;
          &lt;/behavior&gt;
        &lt;/serviceBehaviors&gt;
      &lt;/behaviors&gt;
        &lt;extensions&gt;
      &lt;behaviorExtensions&gt;
        &lt;add name="errorHandler"
              type="WcfService3.ErrorHandlerExtension, WcfService3, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" /&gt;
      &lt;/behaviorExtensions&gt;
    &lt;/extensions&gt;
  
      &lt;protocolMapping&gt;
          &lt;add binding="basicHttpsBinding" scheme="https" /&gt;
      &lt;/protocolMapping&gt;    
      &lt;serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" /&gt;
    &lt;/system.serviceModel&gt;
    &lt;system.webServer&gt;
      &lt;modules runAllManagedModulesForAllRequests="true"/&gt;
      &lt;!--
          To browse web app root directory during debugging, set the value below to true.
          Set to false before deployment to avoid disclosing web app folder information.
        --&gt;
      &lt;directoryBrowse enabled="true"/&gt;
    &lt;/system.webServer&gt;
  
  &lt;/configuration&gt;
  </code></pre>
  
  <p>My WCF interface is:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Runtime.Serialization;
  using System.ServiceModel;
  using System.ServiceModel.Web;
  using System.Text;
  
  namespace WcfService3
  {
      [ServiceContract]
      public interface IService1
      {
          [OperationContract]
          [FaultContract(typeof(MyFault))]
          string GetData(int value);
      }
  
      [DataContract]
      public class MyFault
      {
  
      }
  
  }
  </code></pre>
  
  <p>My question is in IErrorHandler in WCF, if there is any exception during the WCF service call, the <code>HandlerError()</code> function is suppose to get called first like the C# windows application UnhandledException class and then the service should crash right? In the code above, during the service call, an exception is thrown but my <code>HandlerError</code> function is not getting called before the exception is thrown? My goal is to log the error and the WCF service can throw the unhandled exception and crash. I was expecting during the debugging that the breakpoint will visit the HandleError function, but that function is not getting called and just an exception shows up?</p>