HandleError Method
Collapse the table of content
Expand the table of content

IErrorHandler.HandleError Method

Enables error-related processing and returns a value that indicates whether subsequent HandleError implementations are called.

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

bool HandleError (
	Exception error
boolean HandleError (
	Exception error
function HandleError (
	error : Exception
) : boolean
Not applicable.



The exception thrown during processing.

Return Value

true if subsequent IErrorHandler implementations must not be called; otherwise, false. The default is false.

Use the HandleError method to implement error-related behaviors such as error logging, system notifications, shutting down the application, and so on.

If no error handler returns true from the HandleError method the exception is considered unhandled, and may result in a aborted System.ServiceModel.InstanceContext and channel when communicating on a session channel or the ServiceBehaviorAttribute.InstanceContextMode property is set to InstanceContextMode.PerSession.

The error parameter is never a null reference (Nothing in Visual Basic) and contains the exception object that was thrown.

The following code example demonstrates a service that implements IErrorHandler that returns only FaultException 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("http://schemas.microsoft.com/2006/04/mex"))
    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"

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

© 2016 Microsoft