|
|||||||||||||||||||||||
|
Managed JScript/ Microsoft Dynamic Languages Dynamic languages have many devoted supporters, as do statically typed languages, and rail cars of ink have been spilled discussing the relative merits of each. The question of which one is better for creating your RIA is rendered moot because existing dynamic language supporters do not need to switch to C# or Visual Basic.NET in order to create a Silverlight application. As can be seen from the sample applications discussed in this article, it is possible to create Silverlight applications using exclusively one of the Microsoft dynamic languages. Most interestingly, two of Microsoft's dynamic languages are Open Source. Both IronPython and IronRuby have been released under the Microsoft Public License. The source code for IronPython is available at CodePlex. The source code for IronRuby is hosted on Rubyforge. Since Managed JScript is not Open Source, only the binary files (Microsoft.JScript.Runtime.dll and Microsoft.JScript.Compiler.dll) are released. The location of the latest versions should always be available from the Silverlight Dynamic Languages SDK site on CodePlex (discussed below). Silverlight Dynamic Languages SDK Dynamic Language Runtime The DLR is contained in two assemblies, Microsoft.Scripting.dll and Microsoft.Scripting.Core.dll. Since neither of these assemblies is included in the Silverlight browser plug-in, it is necessary for a developer to package them with any dynamic language application. This is one of the tasks of the Chiron tool (discussed below). Chiron
Note that Chiron.exe is initially found in the bin folder in the Silverlight Dynamic Language SDK along with several other dll files. Generally, moving Chiron to another location is not possible without also moving all of these other .dll files, although a separate path to these support files can be specified in the Chiron config file. Of course, the location or locations of Chiron and its support files can also be added to the PATH environment variable.
While Python and Ruby developers will likely run Chiron from the command line, most C# and Visual Basic.NET developers will probably find it easiest to use Chiron from inside Visual Studio. (See also the section on IronPython Studio, below) Doing so requires setting three parameters in the Start Options of your dynamic language web application. These parameters can be found in your application's property pages on the Start Options page.
By default, the "Start action" is set to "Use current page". Unfortunately this will not work in a Dynamic Language project because there is no .xap file available. Creating the .xap file is the responsibility of Chiron, whose path must be specified in the first field as the external program to start when the project is run. The command line argument "/b" tells Chiron not only to start the built-in web server but also to launch the user's default browser to display the project's Start Page. The "Working directory" tells Chiron where to find the data files that it will need to build the required .xap file (i.e., the .py (or .rb) and .xaml (if any) files that make up your web application). Running the sample projects provided with the Silverlight Dynamic Language SDK simply requires opening the folder containing the file "index.html" in Visual Studio (File / Open / Web Site). Right click the Project node in the Solution Explorer and open the Property Pages. Set the Start Options as described above and press F5 to run the project. During development (using the /b parameter) the .xap file which is created by Chiron is written only to memory and not to disk. This is convenient because it can speed the performance of the application when testing modifications of the dynamic language code. However, for deployment it is preferable to save the .xap file to disk. This can be achieved by running Chiron from the command line and specifying both the source path (/d[irectory]) and the name to be given to the .xap file (/z[ipdlr]). Chiron will then build the project and include both the DLR assemblies and whatever specific language assemblies are required by your project. If necessary, Chiron will also generate an AppManifest.xaml file.
IronPython Studio Sample Applications
Photoviewer is another SDK sample application built entirely without C# or Visual Basic.NET. Photoviewer performs a flickr search using Ruby and then displays the results using Lightbox, an open source JavaScript project. This application very nicely illustrates the interoperability of Ruby, JavaScript and Silverlight.
Future Developments (C# 4.0) The significance of a dynamic object is that the C# 4.0 compiler will not check any method calls on a dynamic object at compile time, thus deferring resolution until runtime. While this is standard procedure for dynamic languages, it is highly unusual for a static language like C#. In all other respects, however, C# will remain a static language. The purpose of this change is to facilitate C# interaction with these dynamic objects through syntax simplification which can be illustrated in the following code samples: As you can see, methods belonging to a dynamic object can be called directly instead of through a call to Invoke, passing in the actual method as a string as is currently required. This deferral of method dispatch will extend to field and property accesses, indexer and operator calls as well as to delegate invocations. As we all know, cleaner simpler code is easier to write and less error prone. The mere existence of this project signals the importance which has been assigned to dynamic language development at Microsoft. Summary On the other hand, for the C# and Visual Basic.NET developer, dynamic language support creates the opportunity for a whole new set of developers to build Silverlight applications, many of which will be open source and available for inclusion in non-dynamic Silverlight applications. Additionally, where it is desirable to provide end users with built-in scripting capability, this can best be achieved via the use of a dynamic language. Finally, since the demise of the HTML DOM and since the need for client side scripting does not appear to be on the visible horizon, further developments in scripting support promised for inclusion in the next release of the .NET Framework looks to make the task of interoperability with scripting languages even simpler than it is today. Special thanks to Michael Foord for making available to me a pre-publication copy of his excellent book, IronPython in Action. In addition to a comprehensive explanation of IronPython in general—including how to take advantage of the .NET Framework, this book contains a very useful chapter outlining the use of IronPython in Silverlight. The estimated publication date is in early 2009. Links * This article was commissioned by and prepared for Microsoft Corporation. This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS OR IMPLIED, IN THIS SUMMARY.
|
|||||||||||||||||||||||
|
Cal Schrotenboer is a C# developer with experience in building Windows Forms
application front ends for SQL Server databases. He also teaches programming classes at Foothill College in Los Altos Hills, California and Microsoft Network Administration (MCSE) classes at Mission College in Santa Clara. Cal maintains a WPF blog at www.WPFLearningExperience.com. His outside interests include travel
and photography (www.travelswithcal.com).
| |||||||||||||||||||||||
|
Submit article to:
|