Implementing Silverlight Faults in SL 3.0 with RIA Domain Services – Fix 2

Last month i have post article on how to host Silverlight (With RIA Domain services) project on shared domain. In that article (Link is Here ) some web.config settings are recommended to configure RIA end points. While working tonight i have noticed that Silverlight clients are not able to catch exceptions raised by DomainService ! and that’s because we have replaced the RIA default end point configurations via web.config settings. Silverlight version 3 enables support for the Windows Communication Foundation (WCF) SOAP fault programming model, which allows the service to communicate error conditions to the client. We need to perform following steps to enable error catch to Silverlight client. To send faults to a Silverlight client that are accessible to it, an WCF service must modify the way it sends its fault messages. The key change needed is for WCF to return fault messages with a HTTP 200 response code instead of the HTTP 500 response code. This change enables Silverlight to read the body of the message and also enables WCF clients of the same service to continue working using their normal fault-handling procedures.
The modification on the server can be made by defining a WCF endpoint behavior for Silverlight faults. The following code sample shows how to do this.
Create Project Paris.Silverlight and add class SilverlightFaultBehavior
Copy the code as mentioned below (This has been taken from assembly System.Web.Ria 2.0.0.0) or follow link

1.

using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;

namespace Paris.Silverlight
{
    public class SilverlightFaultBehavior : BehaviorExtensionElement, IEndpointBehavior
    {
        public SilverlightFaultBehavior()
        {

        }
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }

        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
        }

        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            SilverlightFaultMessageInspector inspector = new SilverlightFaultMessageInspector();
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(inspector);
        }

        public void Validate(ServiceEndpoint endpoint)
        {
        }

        public override System.Type BehaviorType
        {
            get { return typeof(SilverlightFaultBehavior); }
        }

        protected override object CreateBehavior()
        {
            return new SilverlightFaultBehavior();
        }
    }

    public class SilverlightFaultMessageInspector : IDispatchMessageInspector
    {
        object IDispatchMessageInspector.AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            // Do nothing to the incoming message
            return null;
        }

        void IDispatchMessageInspector.BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
        {
            if (reply.IsFault)
            {
                HttpResponseMessageProperty property = new HttpResponseMessageProperty();
                property.StatusCode = System.Net.HttpStatusCode.OK; // 200

                reply.Properties[HttpResponseMessageProperty.Name] = property;
            }
        }

    }
}


Compile the project and add reference of above assembly to your RIA web service project.

Now we need to register the custom behaviorExtension element in web.config before doing that i would recommend to find full Qualified name of your assembly holding above class.

Type code mentioned below to your start up aspx page

string name = typeof(Paris.Silverlight.SilverlightFaultBehavior).AssemblyQualifiedName;

copy the value of name , in my case thay are
Paris.Silverlight.SilverlightFaultBehavior, SilverlightFaultBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null

Note that you need to copy the same to web .config. Service may not work if you may put extra space or new line character in web .config.

When registering a custom behaviorExtension element in your app.config to set up a custom endpoint behavior
for a WCF service, WCF seems to require that the type attribute of the behaviorExtension matches *exactly* the assembly qualified name of the BehaviorExtensionElement class at a string level.

2. Add RIASilverlightFaultBehavior to your web.config and also add behaviorConfiguration=”RIASilverlightFaultBehavior” to each endpoint of RIA service where address=”/binary”

<service name="SparkExams.Web.UserRegistrationService" behaviorConfiguration="RIAServiceBehavior">
  <endpoint address="" binding="wsHttpBinding" contract="SparkExams.Web.UserRegistrationService" />
  <endpoint address="/soap" binding="basicHttpBinding" contract="SparkExams.Web.UserRegistrationService"/>
  <endpoint address="/binary" binding="customBinding"  bindingConfiguration="BinaryHttpBinding"
  contract="SparkExams.Web.UserRegistrationService"  behaviorConfiguration="RIASilverlightFaultBehavior"/>
</service>

Don’t forget to deploy SilverlightFaultBehavior assembly to your web server.
Please also note that type attribute is space sensitive, use typeof(className).AssemblyQualifiedName to find type .
Code and article has been tested against Vs 2008, and RIA 2.0 Beta.

Click here for details on Fault Strategies in SL3

10 thoughts on “Implementing Silverlight Faults in SL 3.0 with RIA Domain Services – Fix 2

  1. you can host it in same assembly,however in that case you need to write the same code again and again in each silverlight project.If you compile in sperate assembly you can use it to add refrence to each silverlight project.

    Like

  2. i actually enjoy your own posting kind, very charming,
    don’t quit and also keep writing for the simple reason that it just simply truly worth to read it,
    impatient to looked over far more of your own posts, goodbye 🙂

    Like

  3. My name is Piter Jankovich. oOnly want to tell, that your blog is really cool
    And want to ask you: is this blog your hobby?
    P.S. Sorry for my bad english

    Like

  4. Excellent piece, this is very similar to a site that I have. Please check it out sometime and feel free to leave me a comenet on it and tell me what you think. Im always looking for feedback.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s