65.9K
CodeProject is changing. Read more.
Home

Error handling with Silverlight and WCF

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.18/5 (7 votes)

Sep 20, 2009

CPOL

1 min read

viewsIcon

20930

A better way to handle errors and provide a useful message back to the user in Silverlight.

I was recently working on a big Silverlight 3 project. While trying to figure out how to get error messages to the user, I found out that simply throwing an exception won't return to the client as expected.

The error looks like this and contains non of the exception information you would expect:

The remote server returned an error: NotFound.

That's not very helpful. I need a better way to handle errors and provide a useful message back to the user. My first thought was adding error properties to each return object. That wouldn't work though if I just wanted to return a simple boolean or return nothing at all.

The best solution I see is to use an out parameter. I put together an error class to handle this:

   1: [DataContract]
   2:  public class WCFError
   3:  {
   4:      public WCFError(string Message, string Type)
   5:      {
   6:          this.Message = Message;
   7:          this.Type = Type;
   8:      }
   9:  
  10:      [DataMember]
  11:      public string Message { get; set; }
  12:  
  13:      [DataMember]
  14:      public string Type { get; set; }
  15:  
  16:      [DataMember]
  17:      public string UserMessage
  18:      {
  19:          get
  20:          {
  21:              return "The following error has occured: " + Message;
  22:          }
  23:          set { }
  24:      }
  25:  
  26:  }

Now that I have the error class, I can use it in my service. All I have to do is add an out parameter of type WCFError and it will be available in Silverlight.

   1: [OperationContract]
   2:   public string DoWork(out WCFError error)
   3:   {
   4:       error = null;
   5:  
   6:       try
   7:       {
   8:           //do some data work
   9:           //if error in database
  10:           error = new WCFError("Table not found", "Database");
  11:           return null;
  12:       }
  13:       catch (Exception ex)
  14:       {
  15:           error = new WCFError(ex.Message, "Application");
  16:           return null;
  17:       }
  18:   
  19:   }
   1: void clnt_DoWorkCompleted(object sender, 
           SilverlightApplication1.ServiceReference1.DoWorkCompletedEventArgs e)
   2: {
   3:     if (e.error != null)
   4:     {
   5:         MessageBox.Show(e.error.UserMessage);
   6:         return;
   7:     }
   8:     if (e.Result != null)
   9:     {
  10:         string t = e.Result;
  11:     }
  12: }

So now, through the completed event args, I have access to the out parameter. I should note that, by default, there is an Error parameter added by WCF, so error isn’t a great choice for the parameter name like I did in this example. You could name your out parameter Fault so it is less confusing.

By using an out parameter, I was able to add error handling to my service with little work. For the time being, I feel this is the best option you have to get users a meaningful message in the event of an exception or any other error in the service.