I have a WCF/REST Web Service that I\'m trying to add a global exception handler to. I\'m looking for something similar to the Application_Error event in a standard .NET web
The wiring up of your IServiceBehaviour
can be achieved by creating a custom WebServiceHostFactory
that overrides CreateServiceHost
.
For example if you have a class GlobalErrorHandlerBehaviour
which implements IServiceBehavior
, then you could wire it up as follows:
public class CustomWebServiceHostFactory : WebServiceHostFactory
{
protected override ServiceHost CreateServiceHost(System.Type serviceType, System.Uri[] baseAddresses)
{
return ApplyGlobalErrorHandler(base.CreateServiceHost(serviceType, baseAddresses));
}
private ServiceHost ApplyGlobalErrorHandler(ServiceHost serviceHost)
{
serviceHost.Description.Behaviors.Add(new GlobalErrorHandlerBehaviour());
return serviceHost;
}
}
You would then update your call to the ServiceRoute
constructor to pass in this custom factory.