Technical & Miscellaneous Ramblings
RSS icon Email icon Home icon
  • Handling WCF Exceptions in Silverlight

    Posted on April 18th, 2009 Dhaneel 8 comments

    As you know it’s not possible to handle WCF Exceptions on Silverlight side. As Silverlight plug in does not capture error thrown in 400-500 soap error range. It can only handle soap fault which are 200. So to handle any type of exceptions from WCF we need a work way round. We need to convert exception to 200 and use custom binding at Silverlight side. Here is the attached source code which will do the work for us. In Silverlight 2.0 we don’t have FaultException so attached module contain SLFaultException which can be used to pass custom error messages to silverlight side.

    There are few changes required to adopt this to any silverlight application.
    Add the following to Global.asax which is handling the job of converting fault service request to 200 so that silverlight plug in can handle it.

    protected void Application_EndRequest(object sender, EventArgs e) {
    if (HttpContext.Current.Request.PhysicalPath.EndsWith(".svc", StringComparison.OrdinalIgnoreCase) &&
    HttpContext.Current.Response.StatusCode == 500 &&
    !HttpContext.Current.Request.Browser.Crawler &&
    HttpContext.Current.Request.Browser.EcmaScriptVersion.Major > 0) {
    // Set 200 if its a faulted service request
    HttpContext.Current.Response.StatusCode = 200;
    }
    }

    and In app.xaml we need to register this new assembly

    // Register faults
    System.ServiceModel.SilverlightFaultMessageInspector.RegisterCurrentAssembly();

    and please note the binding It’s a custom httpBinding

    EndpointAddress address = new EndpointAddress("http://127.0.0.1:52620/Service.svc");
    BasicHttpMessageInspectorBinding binding = new BasicHttpMessageInspectorBinding(new SilverlightFaultMessageInspector());
    ServiceClient proxy = new ServiceClient(binding, address);

    That’s it. Now Silverlight application can handle Fault Exceptions from WCF.

    Source :
    SilverlightFaultsSource.zip

    To learn Silverlight follow this link.. http://www.dhaneel.com/whysilverlight/Which will give you a clear picture about silverlight.

     

    8 responses to “Handling WCF Exceptions in Silverlight”

    1. Hi, interesting post. I have been wondering about this issue,so thanks for writing. I will certainly be coming back to your blog.

    2. Hi, interesting post. I have been wondering about this issue,so thanks for sharing. I will likely be coming back to your site. Keep up the good posts

    3. The article is ver good. Write please more

    4. The article is usefull for me. I’ll be coming back to your blog.

    5. any changes coming ?

    6. I’m using the SL 3 as client and the security mode TransportWithMessageCredential. What I need to change for the solution works? I added in the constructor of the class BasicHttpMessageInspectorBinding the command line below:

      base.Security.Mode = BasicHttpSecurityMode.TransportWithMessageCredential;

      Because, without this, the scheme HTTPS is not accepted. But I’m still receiving a CommunicationException in my client. I lose the original error! I removed some parameters from the stored procedure and the method of the service that uses this sp returns a SqlException. I checked the message in service-side and it’s ok. But the client can’t request the original error.

    7. Hi. I like the way you write. Will you post some more articles?

    8. This article was a workway in SL 2.. but in SL3 we have faultException so you can use that..