Wcf Fault convert, Errorhandler extension

When Wcf service throw an exception, if will go down to client and kill its process if client didn’t handle this type of exception.

We need to wrap/convert some exception to FaultException<exception> to avoid this native unfriendly behavior.

Unless FaultException<SomeTypeOfError> is declared in ServiceContract, the fault will reach to client side in the format of FaultException, with no embedded Detail exception object in it.

This embeded SomeTypeOfError doesn’t need to be an exception type, it only needs to be declared as a serializable class.

I did have problem when using exception as the FaultError Type, the service proxy can’t generate any code but the exception at client side. I haven’t see any benefit of pass Exception as FaultDetail (prompt exception?), so it’s not a big problem for me, yet.

An ErrorHandler extension can be used to at service side, to wrap the specific exception to Fault through attribute syntax at class level, which will make service code much clean.

ErrorHandler extension sample code can be downloaded from IDesign website, it only converts the exception declared in Contract to Fault. There is another example which is converting all exception to Fault, similar as I’m doing in my project. I think any kind of the service exception should not kill client process, no matter it’s dumb or smart in handling exception. I’ve changed convert all exception to convert only some known exceptions to fault, unknown/unhandled  exception should pass to client as it is. In fact WCF will convert them to FaultException automatically.

It would be nice if we can do similar attribute syntax at client proxy side as well. The CallBack stuff from IDesign is overkill for this requirement. Some simple attribute extension shouldn’t be very hard to implement. Using PostSharp OnExceptionAspect is the easiest solution.

        public EmailRoleDto AddNewEmailRole(string roleName)

                return _underlyingConnection.AddNewEmailRole(roleName);
            catch (Exception ex)
// Note: HandleException() only handle those exceptions need to handle like validation or security
// while other unknown exception should just let them go to kill the app.
// UI should deal with those seperatelly.

Can be simplified to

 public EmailRoleDto AddNewEmailRole(string roleName)
 return _underlyingConnection.AddNewEmailRole(roleName);

 public class WcfProxyErrorHandlerAttribute : OnExceptionAspect
 public override void OnException(MethodExecutionEventArgs eventArgs)
 // default is ReThrowException
 eventArgs.FlowBehavior = FlowBehavior.Return;

 var errorHandlerHelper = ObjectFactory.GetInstance();



But pre-build PostSharp is pretty hard to implement in Nant, here are some reasons why we should avoid using PostSharp to handle exceptions.

Exception Application Block from EntLib is another option.

ErrorHandler extension

One thought on “Wcf Fault convert, Errorhandler extension

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s