Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF Exception Handling Strategies

Tags:

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

like image 828
xximjasonxx Avatar asked Mar 04 '10 14:03

xximjasonxx


People also ask

What is exception handling in WCF?

Advertisements. A WCF service developer may encounter some unforeseen errors which require reporting to the client in a suitable manner. Such errors, known as exceptions, are normally handled by using try/catch blocks, but again, this is very technology specific.

Which type of exception can be thrown from WCF service?

Using FaultExceptionFault exceptions are exceptions that are thrown by a WCF service when an exception occurs at runtime -- such exceptions are typically used to transmit untyped fault data to the service consumers.


2 Answers

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.

like image 74
lee-m Avatar answered Oct 05 '22 23:10

lee-m


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<T>.

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<T> 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.

like image 26
xximjasonxx Avatar answered Oct 05 '22 23:10

xximjasonxx