Encapsulating conversion of managed Exceptions to WCF Fault Exceptions

Topics: Service Factory Modeling Edition Forum
Sep 16, 2008 at 12:47 AM
I want to adopt a concise and consistent approach to converting managed .NET exceptions into WCF Service Fault
Exceptions. For example, if I have a CustomerNotFoundException exception that gets thrown by a Data Access layer, and I want to catch it and rethrow as a Fault contract you previously defined, to allow WCF
Service clients to be notified of the exception based on recommended WCF practices.

My current solution is to use the Factory pattern with a class that encapsulates the code for converting from managed .NET exceptions to defined Fault contract exceptions. If no custom Fault contract is
defined to match the managed exception then a default FaultException is created instead. The benefit of using the Factory pattern to achieve
this is that the code in your try-catch blocks is always the same.

Therefore My question is: Is this an appropriate solution? Or is their another way to 'map' managed exceptions coming from other layers in your code to Fault Exceptions in WCF?
The FaultExceptionFactory code (C#) that implements this approach is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
//TODO: Add any other relevant using statements here for your own implementations.

namespace BlueTemple.Examples
{
     /// <summary>
     /// Creates WCF Service <c>FaultException</c> objects from managed <c>Exception</c> objects by matching known WCF Fault contracts.
     /// If no matching Fault contract exists <c>Exception</c> is returned instead.
     /// </summary>
     /// <example>
     /// try
     /// {
     ///     // Call to other code e.g. Data Access Layer
     /// }
     /// catch(Exception ex)
     /// {
     ///      throw FaultExceptionFactory.Create(ex);
     /// }
     /// </example>
     static class FaultExceptionFactory
     {
         public static FaultException Create(Exception sourceException)
         {
             FaultException faultException = null;

             if (sourceException is CustomerAlreadyExistsException)
             {
                 CustomerAlreadyExistsFault Fault = new CustomerAlreadyExistsFault();
                 Fault.Message = sourceException.Message;
                 faultException = new FaultException<CustomerAlreadyExistsFault>(Fault, "Customer already exists!");
             }

             if (faultException == null)
             {
                 // If no match with a known Fault contract then use a new Exception with the message only so as to hide sensitive data
                 // from the client.
                 faultException = new FaultException<Exception>(new Exception(sourceException.Message), sourceException.Message);
             }

             return faultException;
         }
     }
}

Sep 16, 2008 at 7:54 PM
From the home page:

  • EntLib Extensions Our Dutch brethren (and advisors) over at Avanade have created and released an extension to the Service Factory: Modeling Edition that allows Enterprise Library to be used in very powerful ways. We would highly encourage you to check out the work they've done. They have a downloadable binary, a walkthrough of how to use this new capability, and an MSDN article that covers it. Check it out and give them some feedback. Great job guys! Well done! http://codeplex.com/EntLibExtensionsWSSF (23 Apr 2008)