Click here to Skip to main content
15,867,974 members
Articles / Programming Languages / C#

Error handling with Silverlight and WCF

Rate me:
Please Sign up or sign in to vote.
4.18/5 (7 votes)
20 Sep 2009CPOL1 min read 20.6K   19   2
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:

C#
 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.

C#
 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:   }
C#
 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)


Written By
Software Developer (Senior) Anytime Fitness
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 1 Pin
codegeha1-Aug-11 9:06
codegeha1-Aug-11 9:06 
GeneralMy vote of 4 Pin
ali_r1427-Apr-11 22:12
ali_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.