{"id":19,"date":"2010-07-03T12:37:31","date_gmt":"2010-07-03T12:37:31","guid":{"rendered":"http:\/\/sujitksingh.com\/?p=19"},"modified":"2011-05-19T16:14:58","modified_gmt":"2011-05-19T16:14:58","slug":"silverlight-hosting-in-winform-application","status":"publish","type":"post","link":"https:\/\/www.sujitksingh.com\/index.php\/silverlight-hosting-in-winform-application\/","title":{"rendered":"Silverlight hosting in WINFORM application"},"content":{"rendered":"<p><strong>Introduction: <\/strong>Silverlight and windows based applications are totally different things. These not only run in separate processes but they are different in the nature also. Windows application runs in full trust mode and could access local resources. In other hand Silverlight applications are meant to be web based and they runs inside a web browser in isolation (sandbox environment).<\/p>\n<p>Silverlight applications are becoming more popular everyday because of ease to develop and user experience. People more often consider developing a web component to be reuse in windows forms environment. It mostly considered as a good option for migration projects where a big enterprise system is migrated in phases.<\/p>\n<p><strong>Possible solution(s)<\/strong><\/p>\n<p>In order to host a Silverlight application within a windows form there are two options.<\/p>\n<ol>\n<li><strong>Silverlight in Winform:<\/strong>Host web browser control in      winform and communicate with Silverlight application.<\/li>\n<\/ol>\n<p>While designing the Silverlight component or application, design everything carefully utilizing shared classes and linked classes concept of VS.net. It will help putting complex code in common classes to be referenced in a Silverlight and WPF based projects. WPF component could be easily hosted inside a typical winform application. Shared classes will be useful to ensure any change done from Silverlight perspective is not breaking WPF and vice-a-versa.<\/p>\n<ol>\n<li><strong>WPF and Silverlight:<\/strong>Design the application in      such a way that common components could be created in form of shared      classes to help building a Silverlight and WPF specific control.<\/li>\n<\/ol>\n<p>While designing the Silverlight component or application, design everything carefully utilizing shared classes and linked classes concept of VS.net. It will help putting complex code in common classes to be referenced in a Silverlight and WPF based projects. WPF component could be easily hosted inside a typical winform application. Shared classes will be useful to ensure any change done from Silverlight perspective is not breaking WPF and vice-a-versa.<\/p>\n<p>There could be other ways of achieving this by separating the communication between two layers by implementing some protocol\/process. An approach where windows application could pass information through server. (These will require additional round trip to server)<\/p>\n<p><strong>Option 2<\/strong>is more about planning the structure of the code and introducing a proper development processes specific to project&#8217;s need.<\/p>\n<p><strong>Option 1 <\/strong>is often seen as a quick win to host existing Silverlight application in a windows application.<\/p>\n<p><strong>Silverlight in Windows Application:<\/strong><\/p>\n<p>Following are the steps for accomplishing option 1 approach of solution.<\/p>\n<ol>\n<li>Within Silverlight      application create a dedicated class to communicate with windows      application and write all the logic there. Communication between      Silverlight and Windows app is necessary to pass information, invoke the      method and raise events etc. Let us name it WindowsClient.cs (in      Silverlight project)<\/li>\n<li>Include reference of      System.Windows.Browser namespace in the class and mark class as      [ScriptableType] to expose the class in the web browser.<\/li>\n<li>Register this class and give      a name to refer the class from web browser using scripting technique      (javascript). If we want to call this class as WinClient, we can register      it as <strong>HtmlPage.RegisterScriptableObject(&#8220;WinClient&#8221;, this);<\/strong> Now this class will be accessible from javascript and DOM through browser.<\/li>\n<li>We can create member      functions to read from Silverlight and to pass information into      Silverlight. Keep in mind that more simple work will leave less security      loophole. Simple code will be easy to maintain as well. Make it a practice      that information flow should be always from windows client to Read and      Push information.<\/li>\n<li>Let us create a method to      accept the information (serialized .net class containing any information      to be used in Silverlight application). See following example and notice      ScriptableMember attribute highlighted.<\/li>\n<\/ol>\n<p>[<strong>ScriptableMember<\/strong>]<br \/>\nPublic void SetInfomation(object serializedInfomation)<br \/>\n{<br \/>\nString message = serializedInformation.ToString();<br \/>\n<em>\/\/there is nothing to stop using a Serialization\/DeSerialization wrapper to pack and \/\/unpack information coming from win form application as following<\/em><br \/>\n<em>\/\/ EventInformation evt = \/\/CustomSerialization.DeserializeEntity&lt;EventInformation&gt;(message);<\/em><br \/>\n\/\/now you have information in Silverlight layer so you can use it as per app&#8217;s need<br \/>\n}<\/p>\n<ol>\n<li>Another important thing is      to raise some notification or event from Silverlight to notify windows      host regarding change in the status. Such event could be session time out,      validation error, information message etc. This could be achieved by writing      a method to return string of serialized class from Silverlight. When      windows client will get notification of any such event, it will read the      Silverlight method to see detail and type of information.<\/li>\n<\/ol>\n<p>[ScriptableMember]<br \/>\nPublic string ReadInformationMessage()<br \/>\n{<br \/>\nString usefullInformation = CustomSerialization.SerializeEnttiy&lt;SessionTimedOut&gt;(App.mySessionObject);<br \/>\nReturn usefullInformation;<br \/>\n<em>\/\/Point to notice here is  that your POCO class being return could contain message, message \/\/type (error, warning, information etc) and anything else<\/em><br \/>\n<em>\/\/windows host application should have a utility to transform these messages and execute \/\/relevant code<\/em><br \/>\n}<\/p>\n<ol>\n<li>In windows application      create a control to host Silverlight application. Add a web browser      control in this custom control.<\/li>\n<li>Declare a private variable      to contain Silverlight application URL.<\/li>\n<li>Modify the constructer to      accept Silverlight application URL. (Alternatively there could be a public      method created to inject the URL and force browser to navigate).<\/li>\n<li>Write the code to navigate      browser control to the given URL. Make sure you have registered event      handlers of web browser control to handle DocumenCompleted and      StatusTextChanged. In order to avoid HTML dialogs appearing to display and      script error make webBrowserControl.ScriptErrorsSuppressed = true.<\/li>\n<li>In the DocumentCompleted      event handler check and ensure whether document is loaded and then push      your information (serialized entity) into Silverlight by using javascript.      Javascript method could be invoked as following example.<\/li>\n<\/ol>\n<p>String strToBePassed = YourSerializationUtil.Serialze&lt;YourObject&gt;(obj);<br \/>\nobject[] script = { &#8220;JSMethodSetInformation(&#8216;&#8221; + strToBePassed + &#8220;&#8216;);&#8221; };<br \/>\nwebHost.Document.InvokeScript(&#8220;eval&#8221;, script);<\/p>\n<p>Alternatively you can directly call you javascript method without using Eval by passing array of object as argument.<\/p>\n<ol>\n<li>In the head section of the      html page (or aspx page) writes following code to initialize Silverlight      object reference. See example below:<\/li>\n<\/ol>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nvar slCtl = null;<br \/>\nfunction initializePlugin(sender, args) {<br \/>\nslCtl = sender.getHost();<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<ol>\n<li>Call above mentioned method      using Silverlight plugin&#8217;s param tag as following:<\/li>\n<\/ol>\n<p>&lt;object id=&#8221;slShell&#8221; data=&#8221;data:application\/x-silverlight-2,&#8221; type=&#8221;application\/x-silverlight-2&#8243; width=&#8221;100%&#8221; height=&#8221;100%&#8221;&gt;<br \/>\n&lt;%&#8211; &lt;param name=&#8221;Windowless&#8221; value=&#8221;true&#8221;\/&gt;&#8211;%&gt;<br \/>\n&lt;param name=&#8221;source&#8221; value=&#8221;ClientBin\/ShellModule.xap&#8221;\/&gt;<br \/>\n&lt;param name=&#8221;onError&#8221; value=&#8221;onSilverlightError&#8221; \/&gt;<br \/>\n&lt;param name=&#8221;background&#8221; value=&#8221;white&#8221; \/&gt;<br \/>\n&lt;param name=&#8221;minRuntimeVersion&#8221; value=&#8221;3.0.40624.0&#8243; \/&gt;<br \/>\n&lt;param name=&#8221;autoUpgrade&#8221; value=&#8221;true&#8221; \/&gt;<br \/>\n&lt;param name=&#8221;onLoad&#8221; value=&#8221;initializePlugin&#8221; \/&gt;<br \/>\n&lt;a href=&#8221;http:\/\/go.microsoft.com\/fwlink\/?LinkID=149156&amp;v=3.0.40624.0&#8243; style=&#8221;text-decoration:none&#8221;&gt;<br \/>\n&lt;img src=&#8221;http:\/\/go.microsoft.com\/fwlink\/?LinkId=108181&#8243; alt=&#8221;Get Microsoft Silverlight&#8221; style=&#8221;border-style:none&#8221;\/&gt;<br \/>\n&lt;\/a&gt;<br \/>\n&lt;object&gt;<\/p>\n<p>In this example, whole object block is given to help understanding usages of Param tag.<\/p>\n<ol>\n<li>Now define Javscript version      of methods to be called from winform application and invoke Silverlight      method.<\/li>\n<\/ol>\n<p>&lt;script type=&#8221;text\/javascript&#8221;&gt;<br \/>\nfunction SetSessionInformation(value)<br \/>\n{<br \/>\nif (slCtl != null)<br \/>\nif (slCtl.Content != null)<br \/>\nslCtl.Content.WinClient.SetInformation(value.toString());<br \/>\n}<\/p>\n<p>function GetInformation(value) {<br \/>\nif (slCtl != null)<br \/>\nif (slCtl.Content != null)<br \/>\nvalue = slCtl.Content. WinClient.GetInformation();<br \/>\nreturn value;<br \/>\n}<br \/>\n&lt;\/script&gt;<\/p>\n<ol>\n<li>In the above mentioned      example it is assumed that there would be two public methods available in      Silverlight&#8217;s class as scriptable member with name of SetInformation and      GetInformation to push and pull the information.<\/li>\n<li>Another interesting thing is      to notify the windows application about any event in Silverlight layer. In      Silverlight if there is any event occurred such as Session time out      following code could be used to change the status of web browser control.<\/li>\n<\/ol>\n<p><strong> HtmlPage.Window.SetProperty(&#8220;status&#8221;, &#8220;Silverlight Status Changed&#8221;);<\/strong><\/p>\n<ol>\n<li>Changing the status message      will trigger web browser&#8217;s event registered in windows form control      (StatusTextChanged). Windows form class than read the Silverlight      information as per status changed notification and execute the appropriate      code.<\/li>\n<\/ol>\n<p><strong>Further Suggestions:<\/strong><\/p>\n<p>These steps are just proof of concept to give a high level view for achieving Silverlight applications within the windows application. As per any existing architecture or guidelines this approach could be extended to fit in the requirement.<\/p>\n<p>It is highly recommended to place these units totally separate to any other part of the application to avoid making these as a dependency of other part of the system. Specific classes could be developed to carry application specific information such as state of business entity, actions, messages (error, warning, information) etc.<\/p>\n<p>It is suggested to encrypt the message while passing between these applications to avoid any data security implications. For general applications standard Base64 encryption would be sufficient, however depending on the data sensitiveness any complex logic could be implemented while serialization process.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Silverlight and windows based applications are totally different things. These not only run in separate processes but they are different in the nature also. Windows application runs in full trust mode and could access local resources. In other hand Silverlight applications are meant to be web based and they runs inside a web browser [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[4,31,5],"_links":{"self":[{"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/posts\/19"}],"collection":[{"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/comments?post=19"}],"version-history":[{"count":4,"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":75,"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/posts\/19\/revisions\/75"}],"wp:attachment":[{"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sujitksingh.com\/index.php\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}