Click here to Skip to main content
12,950,055 members (73,265 online)
Click here to Skip to main content
Add your own
alternative version


19 bookmarked
Posted 20 Sep 2009

Error handling with Silverlight and WCF

, 20 Sep 2009 CPOL
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:      }
10:      [DataMember]
11:      public string Message { get; set; }
13:      [DataMember]
14:      public string Type { get; set; }
16:      [DataMember]
17:      public string UserMessage
18:      {
19:          get
20:          {
21:              return "The following error has occured: " + Message;
22:          }
23:          set { }
24:      }
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;
 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:       }
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.


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


About the Author

Software Developer (Senior) Anytime Fitness
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
codegeha1-Aug-11 9:06
membercodegeha1-Aug-11 9:06 
GeneralMy vote of 4 Pin
ali_r1427-Apr-11 22:12
memberali_r1427-Apr-11 22:12 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170524.1 | Last Updated 20 Sep 2009
Article Copyright 2009 by Paul_Wade
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid