<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>MSDN Utopia : Build</title><link>http://blogs.msdn.com/salvapatuel/archive/tags/Build/default.aspx</link><description>Tags: Build</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Build: Accessing the DLR engine in Silverlight 2</title><link>http://blogs.msdn.com/salvapatuel/archive/2008/03/31/build-accessing-the-dlr-engine-in-silverlight-2.aspx</link><pubDate>Mon, 31 Mar 2008 13:22:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8345398</guid><dc:creator>Salva Patuel</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/salvapatuel/comments/8345398.aspx</comments><wfw:commentRss>http://blogs.msdn.com/salvapatuel/commentrss.aspx?PostID=8345398</wfw:commentRss><description>&lt;FONT face=Calibri&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;FONT size=3&gt;&lt;I style="mso-bidi-font-style: normal"&gt;Note: This code has been updated using Silverlight 2 RTM&lt;/I&gt;&lt;I style="mso-bidi-font-style: normal"&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;&lt;?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/I&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;With Silverlight 2 a new set of components have been introduced into the equation, the most important one is the CLR Execution engine, that support in this case the dynamic language runtime (DLR) that we are going to host in this post. The following chart shows the items that have been changed since version 1.0&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; MARGIN: 0cm 0cm 10pt" class=MsoNormal mce_keep="true"&gt;&amp;nbsp;&lt;/P&gt;&lt;/FONT&gt;
&lt;P style="TEXT-ALIGN: center; MARGIN: 0cm 0cm 10pt" class=MsoNormal align=center&gt;&lt;o:p&gt;&lt;FONT size=3 face=Calibri&gt;&lt;IMG style="WIDTH: 472px; HEIGHT: 423px" src="http://5uaopw.blu.livefilestore.com/y1pvtkJMzymZosCMJgZo8g5y-4qDJRXEi16unwWfKr8R_K-9bRgd6t8IPfRFqPUGstoHZL-nxWLoczmCzsIzcwKmA/Silverlight2.jpg" width=472 height=423 mce_src="http://5uaopw.blu.livefilestore.com/y1pvtkJMzymZosCMJgZo8g5y-4qDJRXEi16unwWfKr8R_K-9bRgd6t8IPfRFqPUGstoHZL-nxWLoczmCzsIzcwKmA/Silverlight2.jpg"&gt;&amp;nbsp;&lt;/FONT&gt;&lt;/o:p&gt;&lt;/P&gt;&lt;FONT size=3 face=Calibri&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;The interesting area regarding the DLR is the interoperability options between the silverlight components and scripted languages. In the release 2.0 we are including support for Iron Python, Ruby and Managed JavaScript. These languages are currently offered through codeplex &lt;A href="http://www.codeplex.com/dynamicsilverlight"&gt;&lt;SPAN style="COLOR: blue"&gt;here&lt;/SPAN&gt;&lt;/A&gt;. The support for VBScript has been replaced by the future version of Visual Basic, called VBX, but it won’t be supported until the language is released. This shows one of the other beauties of the DLR, the ability to add new languages on the fly. These languages can be used in two directions, script to silverlight and silverlight to script.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;This post will focus on hosting the scripting engine in your Silverlight application adding dynamic code to the execution, allowing the inclusion of instances into the scripted code. The first thing that we need to do is including the namespaces:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;using&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; Microsoft.Scripting;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="LINE-HEIGHT: 115%; COLOR: blue; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;using&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; Microsoft.Scripting.Hosting;&lt;BR&gt;&lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; Microsoft.JScript.Runtime;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;With these namespaces we have access to the DLR different engines, in this scenario we are going to use the JavaScript engine.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="MARGIN: 0cm 0cm 10pt; mso-margin-top-alt: auto; mso-margin-bottom-alt: auto" class=MsoNormal&gt;&lt;SPAN style="LINE-HEIGHT: 115%; COLOR: #2b91af; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;ScriptRuntime &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; COLOR: black; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes; mso-themecolor: text1"&gt;MyRuntime&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; COLOR: #2b91af; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; = &lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; COLOR: black; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes; mso-themecolor: text1"&gt;JScript.CreateRuntime();&lt;/SPAN&gt;&lt;SPAN style="LINE-HEIGHT: 115%; COLOR: #2b91af; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;With the instance in place now we can retrieve the script engine as follows:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="COLOR: #2b91af; FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;ScriptEngine&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt; JSEngine = MyRuntime.GetEngine(&lt;SPAN style="COLOR: #a31515"&gt;"js"&lt;/SPAN&gt;);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; FONT-SIZE: 10pt; mso-no-proof: yes"&gt;&lt;o:p&gt;&amp;nbsp;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;In this case, my option was managed JavaScript. This object now focuses on the language per se, exploring the class will fascinate you, as you have access to the core execution of a full program, being able to compile code, define scope and execute it. Now the internals of the execution needs to be explored, I have mentioned the scope; you create a new scope using the following command:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #2b91af; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;IScriptScope&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; Scope = JSEngine.CreateScope();&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;Each execution scope represent the environment where your code will run, it allows you to set variables and their values. We can add an object instance that the scripted code will run (similar to executing COM components in the past but now linked to your silverlight object). Let’s suppose that I expose a method that calls a WCF method:&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #2b91af; FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;IRemoteInterface&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; TransparentProxy = MyFactory.CreateChannel();&lt;BR&gt;Scope.SetVariable(&lt;SPAN style="COLOR: #a31515"&gt;"WCFProxy"&lt;/SPAN&gt;, TransparentProxy);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;Now that we have the scope, we can start defining our code. In order to use the code we should use the &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af; FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;ScriptSource&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;object.&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="COLOR: #2b91af; FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;ScriptSource&lt;/SPAN&gt;&lt;SPAN style="FONT-FAMILY: 'Courier New'; COLOR: #2b91af; FONT-SIZE: 10pt; mso-no-proof: yes"&gt; &lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;Code = JSEngine.CreateScriptSourceFromString(“WCFProxy.MyMethod();”);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="TEXT-ALIGN: justify; LINE-HEIGHT: normal; MARGIN: 0cm 0cm 10pt" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;The script engine allows you to load external files as well, just explore the options that it offers. The ScriptSource object allows you to play with the code, like getting individual lines. All this information can be dynamically changed, giving you the flexibility that you need. Now, the final step, we can compile the code to make sure that is correct and execute it (note that you can execute it directly and will be compiled on the fly).&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-bidi-font-family: 'Courier New'; mso-no-proof: yes"&gt;source.Compile()&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt; ; &lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/SPAN&gt;// Optional&lt;BR&gt;Code.Execute(Scope);&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="LINE-HEIGHT: normal; MARGIN: 0cm 0cm 0pt; mso-layout-grid-align: none" class=MsoNormal&gt;&lt;SPAN style="FONT-SIZE: 10pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Courier New'; mso-fareast-language: EN-GB; mso-no-proof: yes"&gt;&lt;BR&gt;&lt;/SPAN&gt;&lt;SPAN style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-fareast-language: EN-GB"&gt;You can shutdown the engine when is not needed using the Shutdown() command. You can see the power of injecting code into your application on the fly and the simplicity of the model. Explore the DLR!&lt;o:p&gt;&lt;/o:p&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8345398" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/salvapatuel/archive/tags/Build/default.aspx">Build</category><category domain="http://blogs.msdn.com/salvapatuel/archive/tags/DLR/default.aspx">DLR</category><category domain="http://blogs.msdn.com/salvapatuel/archive/tags/Silverlight/default.aspx">Silverlight</category><category domain="http://blogs.msdn.com/salvapatuel/archive/tags/Dynamic+Language/default.aspx">Dynamic Language</category></item></channel></rss>