Click here to Skip to main content
Click here to Skip to main content
Go to top

Error handling with Silverlight and WCF

, 20 Sep 2009
Rate this:
Please Sign up or sign in to vote.
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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Paul_Wade
Software Developer (Senior) Anytime Fitness
United States United States
No Biography provided
Follow on   Twitter

Comments and Discussions

 
GeneralMy vote of 1 Pinmembercodegeha1-Aug-11 9:06 
GeneralMy vote of 4 Pinmemberali_r1427-Apr-11 22:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140916.1 | Last Updated 20 Sep 2009
Article Copyright 2009 by Paul_Wade
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid