Rendering My Fault Message

Posted by onefloridacoder on Geeks with Blogs See other posts from Geeks with Blogs or by onefloridacoder
Published on Thu, 17 Jun 2010 19:06:04 GMT Indexed on 2010/06/17 20:24 UTC
Read the original article Hit count: 273

Filed under:

My coworkers setup  a nice way to get the fault messages from our service layer all the way back to the client’s service proxy layer.  This is what I needed to work on this afternoon.  Through a series of trials and errors I finally figured this out.

The confusion was how I was looking at the exception in the quick watch viewer.  It appeared as though the EventArgs from the service call had somehow magically been cast back to FaultException(), not FaultException<T>. 

I drilled into the EventArgs object with the quick watch and I copied this code to figure out where the Fault message was hiding.  Further, when I copied this quick watch code into the IDE I got squigglies.  Poop.

   1: ((System
   2:   .ServiceModel
   3:   .FaultException<UnhandledExceptionFault>)(((System.Exception)(e.Result)).InnerException)).Detail.FaultMessage

I wont bore you with the details but here’s how it turned out.  

EventArgs which I’m calling “e” is not the result such as some collection of items you’re expecting.  It’s actually a FaultException, or in my case FaultException<T>.  Below is the calling code and the callback to handle the expected response or the fault from the completed event.

   1: public void BeginRetrieveItems(Action<ObservableCollection<Model.Widget>> FindItemsCompleteCallback, Model.WidgetLocation location)
   2: {
   3:     var proxy = new MyServiceContractClient();
   4:  
   5:     proxy.RetrieveWidgetsCompleted += (s, e) => FindWidgetsCompleteCallback(FindWidgetsCompleted(e));
   6:  
   7:     RetrieveWidgetsRequest request = new RetrieveWidgetsRequest { location.Id };
   8:  
   9:     proxy.RetrieveWidgetsAsync(request);
  10: }
  11:  
  12: private ObservableCollection<Model.Widget> FindItemsCompleted(RetrieveWidgetsCompletedEventArgs e)
  13: {
  14:     if (e.Error is FaultException<UnhandledExceptionFault>)
  15:     {
  16:         var fault = (FaultException<UnhandledExceptionFault>)e.Error;
  17:         var faultDetailMessage = fault.Detail.FaultMessage;
  18:  
  19:         UIMessageControlDuJour.Show(faultDetailMessage);
  20:         return new ObservableCollection<BinInventoryItemCountInfo>();
  21:     }
  22:  
  23:     var widgets = new ObservableCollection<Model.Widget>();
  24:  
  25:     if (e.Result.Widgets != null)
  26:     {
  27:         e.Result.Widgets.ToList().ForEach(w => widgets.Add(this.WidgetMapper.Map(w)));
  28:     }
  29:  
  30:     return widgets;
  31: }

© Geeks with Blogs or respective owner