views:

481

answers:

3

We are developing a proxy in WCF that will serve as a means of communication for some handhelds running our custom client application. I am curious what error handling strategies people use as I would rather not wrap EVERY proxy call in try/catch.

When I develop ASP .NET I dont catch the majority of exceptions, I leverage Application_Error in Global asax which can then log the exception, send an email, and redirect the user to a custom error landing page. What I am looking for in WCF is similar to this, except that it would allow me to pass a general faultreason to the client from a central location.

Basically I am curious how people centralize their exception handling in WCF apps.

Thanks

+1  A: 

We use the Exception Handling Application block and shield most faults from clients to avoid disclosing sensitive information, this article might be a good starting point for you, as with "best practices" - you should use what fits your domain.

RandomNoob
+2  A: 

You might find the IErrorHandler interface useful here. We've been using this to do pretty much what you mention - centralised exception logging and providing generalised fault reasons without having to litter the code with numerous try/catches to try and deal with the problem locally.

lee-m
Yes this is in fact exactly what I was looking for, took some work to get it going (see below) but its 100% what I need
xximjasonxx
+1  A: 

So here is what I did. We have a few custom exceptions in our application such as BusinessRuleException and ProcessException, WCF supports both FaultException and FaultException

General practice seems to be that you always throw FaultException to the client in the case of a general error or an error that you dont want to display exactly what happened. In other cases you can pass FaultException where T is a class with information about the particular exception.

I created this concept of Violations in the application, which basically meant that any custom exception had a property containing the corresponding Violation instance. This instance was then passed down to the client enabling the client to recognize when a recoverable error had occured.

This solved part of the problem, but I still wanted a general catch all that would allow me to centeralize logging. I found this by using the IErrorHandle interface and adding my own custom error handler to WCF. Here is the code:

public class ServiceHostGeneralErrorHandler : IErrorHandler
{
    public void ProvideFault(Exception ex, MessageVersion version, ref Message fault)
    {
        if (ex is FaultException)
            return;

        // a general message to the client
        var faultException = new FaultException("A General Error Occured");
        MessageFault messageFault = faultException.CreateMessageFault();
        fault = Message.CreateMessage(version, messageFault, null);
    }

    public bool HandleError(Exception ex)
    {
        // log the exception

        // mark as handled
        return true;
    }
}

Using this method, I can convert the exception from whatever it is to something that can be easily displayed on the client while at the same time logging the real exception for the IT staff to see. So far this approach is working quite well and follows the same structure as other modules in the application.

xximjasonxx