Pixytech

Year: 2010

  • Silverlight – Implementing Clipboard support

    Silverlight 4 (currently in beta) adds support for Clipboard! – However no option to access html in clipboard.
    In Silverlight 3 we had copy/paste available in the Textbox, programmatic access could be done, some IE only solution exists via HTML DOM bridge (so not OOB) or cross-browser (involving Flash!). But now the game has changed as we now have an API for multi-platform Clipboard access.
    In this beta, the support is for (Unicode) text-only, and the Clipboard class has 3 static methods:
    • GetText()
    • SetText()
    • ContainsText()
    Clipboard access can only be done from a user initiated action (mouse, keyboard), and user is prompted to acknowledge the first time Clipboard is set or read (once per session).
    Silverlight 3 Clipboard Class


    Below is the class for Clipboard support in silverlight 3 or 4 with an additional method GetHtmlData() to return formated text from clipboard.
    Class Name : Clipboard.cs, Download Source Code.

    public static class Clipboard
    {
    public static void Copy(KeyEventArgs e, string s);
    private static string GetData(string type);
    public static string GetHtmlData();
    public static string GetTextData();
    public static void SetData(string data);
    public static bool IsEnabled;
    }

    Same tech. can be used in Silverlight 4 as this class provides methods like GetHtmlData() which can be used to get Html contents.This could be very helpfull in RichTextBox Editor,i.e if you could write parser for office html (mso) you may write code for copy/paste of contents from office applications to silverlight application.Example office excel cells will be treated as HTML table cells.Copying images from word documents will provide path to imagedata (VML) which can be then uploaded using OpenFileDialog calls..

    Regards
    Rajneesh Noonia

  • 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