IErrorHandler.HandleError Method (Exception)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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

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

abstract HandleError : 
        error:Exception -> bool


Type: System.Exception

The exception thrown during processing.

Return Value

Type: System.Boolean

true if Windows Communication Foundation (WCF) should not abort the session (if there is one) and instance context if the instance context is not Single; 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, and return a value that specifies whether the exception has been handled appropriately.


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 IErrorHandler implementations are called. By default (when the return value is false), if there is an exception, the dispatcher aborts any session and aborts the InstanceContext if the InstanceContextMode is anything other than Single.  The exception is then considered unhandled and any state is considered corrupt.

Return true from HandleError to prevent this default behavior. If any error handler returns true it instructs WCF that it is safe to continue using state associated with the failed request.

If no error handler returns true from the HandleError method the exception is considered unhandled and the default response applies, potentially resulting in an aborted System.ServiceModel.InstanceContext and channel when communicating on a session channel or the ServiceBehaviorAttribute.InstanceContextMode property is not set to InstanceContextMode.Single.

The error parameter is never null and contains the exception object that was thrown.

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.

No code example is currently available or this language may not be supported.

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

No code example is currently available or this language may not be supported.

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.

.NET Framework
Available since 3.0
Return to top