This is a mirror of official site: http://jasper-net.blogspot.com/

Passing WCF Exception to Silverlight

| Tuesday, September 21, 2010
Since it is not possible to catch regular exception from a WCF service in a silverlight application, there is another way to do it by using BehaviorExtensionElement.

1. On Server-side First create a behavior like this:

public class MyFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
   {
       public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
       {
           SilverlightFaultMessageInspector inspector = new SilverlightFaultMessageInspector();
           endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
       }
       public class SilverlightFaultMessageInspector : IDispatchMessageInspector
       {
           public void BeforeSendReply(ref Message reply, object correlationState)
           {
               if (reply.IsFault)
               {
                   HttpResponseMessageProperty property = new HttpResponseMessageProperty();

                   // Here the response code is changed to 200.
                   property.StatusCode = System.Net.HttpStatusCode.OK;
                   reply.Properties[HttpResponseMessageProperty.Name] = property;
               }
           }
           public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
           {
               // Do nothing to the incoming message.
               return null;
           }
       }
       // The following methods are stubs and not relevant.
       public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
       {
       }
       public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
       {
       }
       public void Validate(ServiceEndpoint endpoint)
       {
       }


Read more: Vincent Leung .NET Tech Clips

Posted via email from .NET Info

0 comments: