Skip to content

Custom Error Handler Overview

mikeobrien edited this page Sep 14, 2010 · 2 revisions

Enabling error handling on a service requires that you attach an error handler to all its channel dispatchers. The WcfRestContrib.ServiceModel.Description.ErrorHandlerBehavior handles this for you.

First you must use the WcfRestContrib.ServiceModel.Web.WebServiceHost instead of the stock WebServiceHost (See more about that under WebServiceHost Overview). The reason is that the stock WebServiceHost automatically adds its own error handler which you cannot override. We can turn off the stock error handler with the WCF REST Contrib WebServiceHost by applying the WcfRestContrib.ServiceModel.Web.ServiceConfigurationAttribute to the service and passing true to the customErrorHandler parameter as follows:

[ServiceConfiguration(true)]
public class Books : IBooksService

Error handlers must implement the System.ServiceModel.Dispatcher.IErrorHandler interface and have a parameterless constructor. You can specify this declaratively on a service or service contract with the WcfRestContrib.ServiceModel.Description.ErrorHandlerAttribute as follows:

[ServiceConfiguration(true)]
[ErrorHandler(typeof(MyCustomErrorHandler))]
public class Books : IBooksService

Or in configuration as follows:

<system.serviceModel>
    <extensions>
        <behaviorExtensions>
            <add name="errorHandler" 
                 type="WcfRestContrib.ServiceModel.Configuration.ErrorHandler.BehaviorElement, WcfRestContrib, 
                       Version=x.x.x.x, Culture=neutral, PublicKeyToken=89183999a8dc93b5"/>
        </behaviorExtensions>
    </extensions>
    <serviceBehaviors>
        <behavior name="Rest">
            <errorHandler errorHandlerType="MyCustomErrorHandler, MyLibrary"/>
        </behavior>
    </serviceBehaviors>
</system.serviceModel>

NOTE: The WcfRestConfrib library offers the WcfRestContrib.ServiceModel.Web.WebErrorHandler which adds some restful features and allows you to plug in a custom logger. See more about it under WebErrorHandler Overview.

NOTE: The WcfRestContrib.ServiceModel.Web.WebServiceHost allows you to specify configuration based behaviors if you do not want to specify this declaratively. See more about it under Declarative Binding & Behavior Overview.

Clone this wiki locally