IErrorHandler Interface


Allows an implementer to control the fault message returned to the caller and optionally perform custom error processing such as logging.

Namespace:   System.ServiceModel.Dispatcher
Assembly:  System.ServiceModel (in System.ServiceModel.dll)

public interface IErrorHandler


Enables error-related processing and returns a value that indicates whether the dispatcher aborts the session and the instance context in certain cases.

System_CAPS_pubmethodProvideFault(Exception, MessageVersion, Message)

Enables the creation of a custom FaultException<TDetail> that is returned from an exception in the course of a service method.

To explicitly control the behavior of the application when an exception is thrown, implement the IErrorHandler interface and add it to the ChannelDispatcher’s ErrorHandlers property. IErrorHandler enables you to explicitly control the SOAP fault generated, decide whether to send it back to the client, and perform associated tasks, such as logging. Error handlers are called in the order in which they were added to the ErrorHandlers property.

Implement the ProvideFault method to control the fault message that is returned to the client.

Implement the HandleError method to ensure error-related behaviors, including error logging, assuring a fail fast, shutting down the application, and so on.


Because the HandleError method can be called from many different places there are no guarantees made about which thread the method is called on. Do not depend on HandleError method being called on the operation thread.

All ProvideFault implementations are called first, prior to sending a response message. When all ProvideFault implementations have been called and return, and if fault is non-null, it is sent back to the client according to the operation contract. If fault is null after all implementations have been called, the response message is controlled by the ServiceBehaviorAttribute.IncludeExceptionDetailInFaults property value.


Exceptions can occur after all ProvideFault implementations are called and a response message is handed to the channel. If a channel exception occurs (for example, difficulty serializing the message) IErrorHandler objects are notified. In this case, you should still make sure that your development environment catches and displays such exceptions to you or makes use of tracing to discover the problem. For more information about tracing, see Using Tracing to Troubleshoot Your Application.

After the response message has been sent, all HandleError implementations are called in the same order.

Typically, an IErrorHandler implementation is added to the ErrorHandlers property on the service (and the client in the case of duplex communication).

You can add the IErrorHandler to the runtime by implementing a behavior (either an System.ServiceModel.Description.IServiceBehavior, System.ServiceModel.Description.IEndpointBehavior, System.ServiceModel.Description.IContractBehavior, or System.ServiceModel.Description.IOperationBehavior object) and use the behavior programmatically, from a configuration file or with a custom attribute to attach your IErrorHandler.

For more information about using behaviors to modify the runtime, see Configuring and Extending the Runtime with Behaviors.

The following code example demonstrates a service that implements IErrorHandler that returns only FaultException<TDetail> of type GreetingFault when a service method throws a managed exception.

#region IErrorHandler Members
public bool HandleError(Exception error)
  Console.WriteLine("HandleError called.");
  // Returning true indicates you performed your behavior.
  return true;

// This is a trivial implementation that converts Exception to FaultException<GreetingFault>.
public void ProvideFault(
  Exception error,
  MessageVersion ver,
  ref Message msg
  Console.WriteLine("ProvideFault called. Converting Exception to GreetingFault....");
  FaultException<GreetingFault> fe 
    = new FaultException<GreetingFault>(new GreetingFault(error.Message));
  MessageFault fault = fe.CreateMessageFault();
  msg = Message.CreateMessage(

The following code example shows how to use a service behavior to add the IErrorHandler implementation to the ErrorHandlers property.

// This behavior modifies no binding parameters.
#region IServiceBehavior Members
public void AddBindingParameters(
  ServiceDescription description, 
  ServiceHostBase serviceHostBase, 
  System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints, 
  System.ServiceModel.Channels.BindingParameterCollection parameters

// This behavior is an IErrorHandler implementation and 
// must be applied to each ChannelDispatcher.
public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase)
  Console.WriteLine("The EnforceGreetingFaultBehavior has been applied.");
  foreach(ChannelDispatcher chanDisp in serviceHostBase.ChannelDispatchers)

// This behavior requires that the contract have a SOAP fault with a detail type of GreetingFault.
public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase)
  Console.WriteLine("Validate is called.");
  foreach (ServiceEndpoint se in description.Endpoints)
    // Must not examine any metadata endpoint.
    if (se.Contract.Name.Equals("IMetadataExchange")
      && se.Contract.Namespace.Equals(""))
    foreach (OperationDescription opDesc in se.Contract.Operations)
      if (opDesc.Faults.Count == 0)
        throw new InvalidOperationException(String.Format(
          "EnforceGreetingFaultBehavior requires a "  
          + "FaultContractAttribute(typeof(GreetingFault)) in each operation contract.  "
          + "The \"{0}\" operation contains no FaultContractAttribute.",
      bool gfExists = false;
      foreach (FaultDescription fault in opDesc.Faults)
        if (fault.DetailType.Equals(typeof(GreetingFault)))
          gfExists = true;
      if (gfExists == false)
        throw new InvalidOperationException(
"EnforceGreetingFaultBehavior requires a FaultContractAttribute(typeof(GreetingFault)) in an operation contract."

The following code example shows how to configure the service to load the service behavior using an application configuration file. For more details about how to expose a service behavior in a configuration file, see IServiceBehavior.

            <add baseAddress="http://localhost:8080/SampleService"/>
        <behavior name="metaAndErrors">
          <serviceDebug includeExceptionDetailInFaults="true"/>
          <serviceMetadata httpGetEnabled="true"/>
          <enforceGreetingFaults />
          type="Microsoft.WCF.Documentation.EnforceGreetingFaultBehavior, HostApplication, Version=, Culture=neutral, PublicKeyToken=null"

.NET Framework
Available since 3.0
Return to top