using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel.Description; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Dispatcher; using System.Collections.ObjectModel; using System.Net; namespace YourNS { /* * To have this error Behavior on the service, use the following line in the service implementation * [ErrorBehavior(typeof(HttpErrorHandler))] */ public class ErrorBehaviorAttribute : Attribute, IServiceBehavior { IErrorHandler errorHandler; //If other parameters are needed they can be added here public ErrorBehaviorAttribute(Type type) { //New error handlers here if (type == typeof(HttpErrorHandler)) { this.errorHandler = new HttpErrorHandler(); return; } throw new ArgumentException(string.Format("Error Handler of type: {0} isn't registered.", type.ToString())); } public void Validate(ServiceDescription description, ServiceHostBase serviceHostBase) { } public void AddBindingParameters(ServiceDescription description, ServiceHostBase serviceHostBase, Collection endpoints, BindingParameterCollection parameters) { } public void ApplyDispatchBehavior(ServiceDescription description, ServiceHostBase serviceHostBase) { foreach (ChannelDispatcherBase channelDispatcherBase in serviceHostBase.ChannelDispatchers) { ChannelDispatcher channelDispatcher = channelDispatcherBase as ChannelDispatcher; channelDispatcher.ErrorHandlers.Add(this.errorHandler); } } } public class HttpErrorHandler : IErrorHandler { public bool HandleError(Exception error) { return false; } public void ProvideFault(Exception error, MessageVersion version, ref Message fault) { // HackFaults store the exception in the httpcontext var context = System.Web.HttpContext.Current; if (context != null) { // Only works in AspNetCompat mode if (!context.Request.Browser.Crawler && context.Request.Browser.EcmaScriptVersion.Major > 0) { // This should rule out non-browsers context.Items.Add("HackFault", error); } } } } }