How to add global error handling in wcf rest servi

2019-04-10 16:51发布

问题:

In my web applications I make use of the Application_Error function in global.asax to log all exceptions like so:

void Application_Error(object sender, EventArgs e)
{
    Exception ex = Server.GetLastError();

    while (ex.GetBaseException() != null)
    {
        ex = ex.GetBaseException();
    }

    log.writeError(ex.ToString());
}

I've tried similiar in a WCF REST service with no luck. How would I add global error handling? I saw this article, but I'm new to implementing the IServiceBehavior. Where would I add the above code?

回答1:

I use:

1) AppDomain.CurrentDomain.UnhandledException event

2) TaskScheduler.UnobservedTaskException event

3) IErrorHandler:

    public class ErrorHandler : IErrorHandler
    {
        public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
        {
            var faultException = new FaultException<string>("Server error: " + error.Format());
            var messageFault = faultException.CreateMessageFault();
            fault = Message.CreateMessage(version, messageFault, null);
        }

        public bool HandleError(Exception error)
        {
            return false;
            //return true; //if handled
        }
    }

[AttributeUsage(AttributeTargets.Class)]
public class ErrorHandlerBehavior : Attribute, IEndpointBehavior, IServiceBehavior
{
    public void Validate(ServiceEndpoint endpoint)
    {

    }

    public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new ErrorHandler()); 
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {

    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {

    }

    public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
    {

    }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
        foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
        {
            channelDispatcher.ErrorHandlers.Add(new ErrorHandler());
        }
    }
}

This can be applied to entire service impl. class:

[ErrorHandlerBehavior]
    public class SubscriberInfoTaskService : {}

or to endpoint:

var endpoint = Host.Description.Endpoints.FirstOrDefault();

//foreach (ChannelDispatcher channelDispatcher in Host.ChannelDispatchers) //ChannelDispatcherBase
//{                 
//    channelDispatcher.ErrorHandlers.Add(new ErrorHandler());
//}

endpoint.Behaviors.Add(new ErrorHandlerBehavior());

here about using config: http://www.steverb.com/post/2008/11/24/Useful-WCF-Behaviors-IErrorHandler.aspx



标签: wcf rest