The Problem Solver

Tell me and I will forget
Show me and I will remember
Involve me and I will understand
- Confucius -

Google Ads

This Blog

Syndication

Search

Tags

News





  • View Maurice De Beijer's profile on LinkedIn

Community

Email Notifications

Explore

Archives

Returning exception information to a Silverlight client through WCF

Error handling code can be a drag but the simple fact is that everyone needs to write it because runtime errors can and do occur. Developing a Silverlight app is no different here, exceptions will occur and you as a developer will need to deal with them.

One of the common things in Silverlight is communicate with a server through WCF and as soon as we start executing code on the server that is just another place where errors can occur. No big deal you might think as that is the same with any other application using WCF. Well the fact that it will also happen in other applications is certainly true but unfortunately the way Silverlight communicates with a WCF service makes live a bit more difficult.

 

But wait, WCF supports a FaultContract so we can just return exceptions as faults right?

Wrong Sad

Unfortunately the Silverlight WCF stack doesn’t support a service contract with a fault contact and just adding the FaultContract attribute will make the service unusable for Silverlight clients. Even when no faults are returned Sad

So the standard WCF faults are out, what else?

Well can’t we just throw an exception and have that resurface in the Silverlight client? Unfortunately we can’t.  Even though the server WCF stack returns the exception just fine, see the Fiddler output, the browser will not pass the information on to the client and inside of Silverlight all we see is a CommunicationException with the very helpful message “The remote server retuned an error: NotFound”. Less than useful Sad

image

So this code will not work

[OperationContract]
public IList<Customer> GetCustomersWithException()
{
    IList<Customer> result = GetCustomers();
 
    throw new FaultException<ArgumentException>(
        new ArgumentException("This is my error text."));
 
    return result;
}

 

So is there no solution?

Well there are a few examples out there that tag an Error property onto the data returned. Guess that works but I kind of don’t like it for a few reasons:

  1. It means you cannot have a simple return value, it must always be a more complicated custom type.
  2. You are mixing good and bad data into the same object and presumably only a single set will be filled.
  3. It therefore breaks the single responsibility rule.

 

So a better solution would be to return two items instead of one.

Turns out we can use out parameters with WCF and they will be wrapped just fine into the return message. So we can return the error information using an out parameter. Great, feels like a much better design Smile

So my first thought was to send the exception as the out parameter and the Silverlight client would get all error information that way. Turns out that didn’t work all that well. Even though the error was passed to the client the message shown in the client was always the default message and not the one actually send. And a second problem was that the parameter type had to mach the exact exception passed. using the KnownType attribute didn’t help there.

So instead of sending the original exception I decided to create a small wrapper type, MyFaultContract, that would pass the exception type and message to the Silverlight client. This info should be more than enough in most cases.

The service operation now looks like this.

[OperationContract]
public IList<Customer> GetCustomersWithCustomError(out MyFaultContract myFault)
{
    IList<Customer> result = null;
    myFault = null;
 
    try
    {
        result = GetCustomersWithException();
    }
    catch (FaultException<ArgumentException> ex)
    {
        myFault = new MyFaultContract()
        {
            FaultType = ex.Detail.GetType().FullName,
            Message = ex.Detail.Message
        };
    }
 
    return result;
}

With the following custom MyFaultContract

[DataContract]
public class MyFaultContract
{
    [DataMember]
    public string FaultType { get; set; }
    [DataMember]
    public string Message { get; set; }
}

 

So what does our client code look like with this extra fault contract?

private void Button_Click_2(object sender, RoutedEventArgs e)
{
    Service1Client proxy = GetService1Client();
 
    proxy.GetCustomersWithCustomErrorCompleted += (s, arg) =>
    {
        if (arg.Error != null)
            ErrorText.Text = arg.Error.GetType().FullName + 
                "\n" + arg.Error.Message;
 
        else if (arg.myFault != null)
            ErrorText.Text = arg.myFault.FaultType + 
                "\n" + arg.myFault.Message;
 
        else
            DataContext = arg.Result[0];
    };
    proxy.GetCustomersWithCustomErrorAsync();
}

Not bad. It does take an extra check as we have to check both the Error and myFault properties for errors but other than that it is exactly the same.

 

So not a perfect solution but it works well enough for most purposes!

Enjoy!

Published Tue, Jan 27 2009 19:19 by Maurice
Filed under: , , ,

Comments

# re: Returning exception information to a Silverlight client through WCF@ Wednesday, January 28, 2009 6:56 AM

Nice! Simple and effective. I can live with a tweaked service contract as long as I don't have to touch my business objects to make it work with Silverlight.

# re: Returning exception information to a Silverlight client through WCF@ Tuesday, February 10, 2009 2:52 AM

it's so bad that I'm also facing the same probelm.. but I don't think that we can change our WCF code like that. It's kinda extra effort to modify each and every WCF methods

# Silverlight Travel &raquo; Returning exception information to a Silverlight client through WCF@ Tuesday, February 10, 2009 7:20 AM

Pingback from  Silverlight Travel » Returning exception information to a Silverlight client through WCF

# re: Returning exception information to a Silverlight client through WCF@ Thursday, February 12, 2009 7:20 AM

This is extraordinarily helpful. :)

Thanks for your effort.

by Britney Spears

# re: Returning exception information to a Silverlight client through WCF@ Tuesday, February 17, 2009 12:01 AM

Ouch... I'm pretty sure I'm running into this same problem.  Thanks for the explanation. Kinda hard to take it all in though but gives me something to thing about.

by Tony

# re: Returning exception information to a Silverlight client through WCF@ Wednesday, February 18, 2009 10:15 AM

This is very useful.  Thank you.

by Edgar

# Use of [FaultContract] in Silverlight-WCF Service &laquo; configuration: debug@ Friday, March 13, 2009 2:56 PM

Pingback from  Use of [FaultContract] in Silverlight-WCF Service « configuration: debug

# Silverlight Travel &raquo; Use of [FaultContract] in Silverlight-WCF Service@ Saturday, March 14, 2009 12:44 AM

Pingback from  Silverlight Travel » Use of [FaultContract] in Silverlight-WCF Service

# Thank you@ Tuesday, May 12, 2009 3:28 AM

Very creative solution - I like it. The lack of support for custom FaultContracts between WCF and SL is a real pity, and something I hope they consider with the next release.

by Alexis

# cheap propecia buy online@ Saturday, May 30, 2009 3:39 PM

Very interesting site, Hope it will always be alive!

# darrenceke@ Thursday, June 11, 2009 11:37 AM

simulations store intense institute vectors llc

# re: Returning exception information to a Silverlight client through WCF@ Friday, June 12, 2009 12:49 PM

Thank you for this article. Saved me a lot of time.

In case anyone needs to do this in VB,

To create an output parameter in VB.Net, add the

<System.Runtime.InteropServices.Out()> attribute before the parameter definition:

<OperationContract()> _

Public Function MyMethod( _

 ByVal param1 As Short, _

 <System.Runtime.InteropServices.Out()> ByRef fault As ApplicationFault) As ReturnType

by Assaf

# re: Returning exception information to a Silverlight client through WCF@ Sunday, June 14, 2009 7:41 PM

We are also using out but instead we are returning enum type and each flag has its own definition defined under resource file. Silverlight apps gets the flag as per flag as a key.

by Rick

# Xdnnytcn@ Saturday, June 27, 2009 6:19 AM

hUqDla comment5 ,

# re: Returning exception information to a Silverlight client through WCF@ Monday, June 29, 2009 9:42 AM

I forced a SqlException, removing some parameters from my stored procedure.

I used your suggestion in my solution, but every time I have an error in my service, he ever pass in the first "if" (if (arg.Error !=null)) in my event "Completed" in client layer. And debugging the method of the service generated in my client, I saw that the out parameter doesn't receive any value, because the error occurs before.

by TH

# fake valium@ Thursday, July 02, 2009 2:10 PM

Perfect work!

# adipex quick no prescription@ Thursday, July 02, 2009 3:38 PM

Perfect work!

# xanax level blood or urine@ Thursday, July 02, 2009 5:16 PM

Perfect site, i like it!

# valium injectable addictive@ Saturday, July 04, 2009 4:45 AM

Great work, webmaster, nice design!

# ordering adipex online@ Saturday, July 04, 2009 5:55 AM

Beautiful site!

# does xanax cause heart failure@ Saturday, July 04, 2009 7:05 AM

Perfect site, i like it!

# cialis women libido@ Sunday, July 05, 2009 4:50 PM

Perfect site, i like it!

# ultram er abuse@ Sunday, July 05, 2009 6:08 PM

Perfect site, i like it!

# buy adipex si@ Monday, July 06, 2009 4:24 AM

Incredible site!

# paleoclimatology cooling@ Monday, July 06, 2009 7:46 AM

down shelf least offset led sulfate

# cheapest phentermine online cheapest@ Wednesday, July 08, 2009 5:32 PM

Incredible site!

# soma lavender seeds@ Friday, July 10, 2009 3:47 AM

Great site. Keep doing.

# xanax overnighted@ Friday, July 10, 2009 5:08 AM

Perfect site, i like it!

# adipex next day@ Friday, July 10, 2009 10:02 AM

Excellent site. It was pleasant to me.

# article insider adipex diet pills@ Saturday, July 11, 2009 5:45 PM

Very interesting site. Hope it will always be alive!

# Nthbczqc@ Tuesday, July 14, 2009 1:03 AM

KTQwZw