Geeks With Blogs
John Smith's Blog Keeping the fun machine running one nickel at a time

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: }
Posted on Thursday, June 17, 2010 8:06 AM | Back to top


Comments on this post: Rendering My Fault Message

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © onefloridacoder | Powered by: GeeksWithBlogs.net