WCF Client-Side Exceptions (getting full service-side exception detail)

Home / WCF Client-Side Exceptions (getting full service-side exception detail)

WCF throws System.ServiceModel.FaultExceptions (FE) between the client and server. The FE is a System.Exception so the client does not need to explicitly catch FaultException, but the FE is missing something. It does not take an InnerException in its constructor, so the server-side chain of exceptions and stack trace data are not available to the client. The typical response to this is “That’s the way it’s supposed to work!”, but if you own/control the consumers of your web service and you want the exception chain and stack trace data, then you’ll want to use FaultException.

The first thing is to decorate your service operations with the FaultContract attribute:

By default, the service implementation will throw all unhandled exceptions back to the client as a regular FaultException. To avoid this, catch exceptions and re-throw them as FaultException as such…

Great! All that detail is on its way to your client.

FaultException and FaultException<>; are derived from System.Exception, so when you don’t care about the detail, just catch Exception. When you want the detail, catch FaultException and inspect …

An instance of ExceptionDetail stores a copy of the underlying Exception data as well as the Exception’s InnerException data, which is also converted to a ExceptionDetail, thus preserving the data of the Exception chain. This means that custom exception types thrown by the server cannot specifically be caught by the client. It also means that the client does not need to reference these exception types. However, the client can determine the type of exception thrown by the server…

The client wishes to detect the ABC.EmptyGuidException thrown by a library called by the service implementation.

Clunky, but attainable.