Error-Handling Extensions

WCF enables developers to customize the default exception reporting and propagation, and even provide for a hook for custom logging. This extensibility is applied per channel dispatcher, although you are more than likely to simply utilize it across all dispatchers.

To install your own error-handling extension, you need to provide the dispatchers with an implementation of the IErrorHandler interface defined as:

public interface IErrorHandler
{
   bool HandleError(Exception error);
   void ProvideFault(Exception error,MessageVersion version,ref Message fault);
}

Any party can provide this implementation, but typically it will be provided either by the service itself or by the host. In fact, you can have multiple error-handling extensions chained together. You will see later in this section just how to install the extensions.

Providing a Fault

The ProvideFault( ) method of the extension object is called immediately after any exception is thrown by the service or any object on the call chain down from a service operation. WCF calls ProvideFault( ) before returning control to the client and before terminating the session (if present) and before disposing of the service instance (if required). Because ProvideFault( ) is called on the incoming call thread while the client it still blocked waiting for the operation to complete, you should avoid lengthy execution inside ProvideFault( ).

Using ProvideFault( )

ProvideFault( ) is called regardless of the type of exception ...

Get Programming WCF Services now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.