Implementing IErrorHandler for WCF Services C# with Example
Implementing IErrorHandler for WCF services is a great way to centralize error handling and logging. The implementation shown here should catch any unhandled exception that is thrown as a result of a call to one of your WCF services. Also shown in this example is how to return a custom object, and how to return JSON rather than the default XML. Implement IErrorHandler: using System.ServiceModel.Channels; using System.ServiceModel.Dispatcher; using System.Runtime.Serialization.Json; using System.ServiceModel; using System.ServiceModel.Web; namespace BehaviorsAndInspectors { public class ErrorHandler : IErrorHandler { public bool HandleError(Exception ex) { // Log exceptions here return true; } // end public void ProvideFault(Exception ex, MessageVersion version, ref Message fault) { // Get the outgoing response portion of the current context var response = WebOperationContext.Current.OutgoingResponse; // Set the default http status code response.StatusCode = HttpStatusCode.InternalServerError; // Add ContentType header that specifies we are using JSON response.ContentType = new MediaTypeHeaderValue("application/json").ToString(); // Create the fault message that is returned (note the ref parameter) with BaseDataResponseContract fault = Message.CreateMessage( version, string.Empty, new CustomReturnType { ErrorMessage = "An unhandled exception occurred!" }, new DataContractJsonSerializer(typeof(BaseDataResponseContract), new List { typeof(BaseDataResponseContract) })); if (ex.GetType() == typeof(VariousExceptionTypes)) { // You might want to catch different types of exceptions here and process them differently } // Tell WCF to use JSON encoding rather than default XML var webBodyFormatMessageProperty = new WebBodyFormatMessageProperty(WebContentFormat.Json); fault.Properties.Add(WebBodyFormatMessageProperty.Name, webBodyFormatMessageProperty); } // end } // end class } // end namespace In this example we attach the handler to the service behavior. You could also attach this to IEndpointBehavior, IContractBehavior, or IOperationBehavior in a similar way. Attach to Service Behaviors: using System; using System.Collections.ObjectModel; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Configuration; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; namespace BehaviorsAndInspectors { public class ErrorHandlerExtension : BehaviorExtensionElement, IServiceBehavior { public override Type BehaviorType { get { return GetType(); } } protected override object CreateBehavior() { return this; } private IErrorHandler GetInstance() { return new ErrorHandler(); } void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection bindingParameters) { } // end void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { var errorHandlerInstance = GetInstance(); foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers) { dispatcher.ErrorHandlers.Add(errorHandlerInstance); } } void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase) { } // end } // end class } // end namespace Configs in Web.config: ... .... ... Here are a few links that may be helpful on this topic: https://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.ierrorhandler(v=vs.100).aspx http://www.brainthud.com/cards/5218/25441/which-four-behavior-interfaces-exist-for-interacting-with-a-service-or- client-description-what-methods-do-they-implement-and Other Examples: IErrorHandler returning wrong message body when HTTP status code is 401 Unauthorized IErrorHandler doesn't seem to be handling my errors in WCF .. any ideas? How to make custom WCF error handler return JSON response with non-OK http code? How do you set the Content-Type header for an HttpClient request?