-
Handling WCF Exceptions in Silverlight
Posted on April 18th, 2009 8 commentsAs 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.zipTo learn Silverlight follow this link.. http://www.dhaneel.com/whysilverlight/Which will give you a clear picture about silverlight.
Silverlight custom exception from wcf, Exception Handling in silverlight, FaultException, handling custom exceptions from wcf, handling custom exceptions from wcf in silverlight, silverlight exceptions, SLFaultException, soap fault error, WCF Exception in silverlight, web service exception handling8 responses to “Handling WCF Exceptions in Silverlight”
-
Hi, interesting post. I have been wondering about this issue,so thanks for writing. I will certainly be coming back to your blog.
-
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
-
The article is ver good. Write please more
-
The article is usefull for me. I’ll be coming back to your blog.
-
any changes coming ?
-
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.
-
Hi. I like the way you write. Will you post some more articles?
-
Dhaneel August 11th, 2009 at 18:40
This article was a workway in SL 2.. but in SL3 we have faultException so you can use that..
-


