IErrorHandler
Realistically, it is impossible for the service designer to have full knowledge of all the exceptions that may be thrown by downstream business components as the operation executes. I’ve already established that you shouldn’t indiscriminately throw unhandled exceptions as faults. You also don’t necessarily want to wrap all uncaught exceptions as faults. You may, however, want to log all uncaught exceptions and, if appropriate, convert some of those exceptions to faults to prevent the channel from faulting.
Adding common error handling code such as this directly to each service operation would
be cumbersome and unmanageable. For this reason, WCF provides support for error handler
components. Error handlers are types that implement IErrorHandler
and are associated with the service behavior. In this section,
you will learn how to create and configure a custom error handler. In the process, you’ll
learn how to dynamically configure the ServiceHost
using
an IServiceBehavior
extension, using a custom attribute,
and by extending ServiceHost
. You’ll do a lab that
exercises the basic concepts first.
Lab: Intercepting Uncaught Exceptions with IErrorHandler
The solution for this lab is much the same as the other two in this chapter. Through the client interface, you can upload photos to a service, and the service is currently configured with a bad directory name for file uploads. In this lab, you will explore how to create a custom error handler to process uncaught exceptions and ...
Get Learning WCF 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.