Sending customized error/exception to WCF client

I was struggling the similar problem as this one, , my customized exception (inherited from exception class) could not be sent to WCF client, once I changed it to regular exception (?) class, it works  as expected. What’s wrong here?

It turns out it was inheritance problem, I missed 2 things: according MSDN guidline (http://msdn.microsoft.com/en-us/library/ms229064(VS.80).aspx)

  1. My customized exception should implement the common ctor accepting (SerializationInfo info, StreamingContext context)
  2. Customized field in my exception needs special storage process done by GetObjectData, and parsing in the common ctor mentioned in 1.

My exception class:


[Serializable]
 public class MyValidationException : Exception
 {
    public IEnumerable<string> ValidationErrors { get; private set; }

    public MyValidationException(IEnumerable<string> validationErrors)
    {
        ValidationErrors = validationErrors;
    }

    protected MyValidationException(SerializationInfo info, StreamingContext context) : base(info, context)
    {
        ValidationErrors = info.GetString("ValidationErrors").Split(new char[]{'\n'});
    }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        base.GetObjectData(info, context);
        info.AddValue("ValidationErrors", string.Join("\n",ValidationErrors.ToArray()));
    }

Option B might be to create completely seperate Fault class to re-wrap this exception when doing Converting exception to fault, in which we can take the customized field as ctor parameter to my Fault class.


[Serializable]
public class MyValidationFault : MyBaseFault
{
   public IEnumerable<string> Errors { get; private set; }

   public MyValidationFault(IEnumerable<string> errors, string message)
    : base(message)
   {
     Errors = errors;
   }
}

public static FaultException ConvertExceptionToFault(Exception ex)
{
   if (ex is MyValidationException)
   {
     var vex = (MyValidationException)ex;
     return new FaultException<MyValidationError>(
      new MyValidationError(vex.ValidationErrors, ex.Message)
        , "Validation Failed"
      );
   }

    ...

This disadvantage of creating customized fault solution is, if WCF client use service reference to create client proxy, each proxy will save local proxy types into its own namespace, those common fault classes will be duplicate. It might be a good idea to move those fault into a shared project between server and client.

GenericADOException

Many people start using UoW pattern in WCF by different ways, both Andreas and IgooCoder are using WCF extension. IgooCoder Common removes extension in IDispatchMessageInspector.BeforeSendReply() and then commit  UoW in IExtension.Detach(),  while Andeas commits right in BeforeSendReply.

If exception happens in commit, it won’t be caught by provideFault(), because it’s out of method scope. FaultContract defined at method header doesn’t effect thos commit related exception. They will reach down to UI as general FaultException.

To catch those kind of framework level exception at client side,


protected void HandleException(Exception e){
     if (e is FaultException)
     {
            if( e is FaultException<MyValidationFault>)
            { ...
            }
            else
            {
              _messageBox.Show(((FaultException<ExceptionDetail>)e).Detail.InnerException.Message);
              throw e;
            }
            return;
      }

This means we should only define customized exception in FaultContract.

Advertisements

One thought on “Sending customized error/exception to WCF client

  1. Pingback: No configuration Wcf host and client « maonet technotes

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s