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


