Dynamic Silverlight Part 1: Hello, World!
Our team is happy to announce Dynamic Silverlight (DSL), which integrates our DLR dynamic languages with Silverlight. It requires Silverlight 2, which includes the cross-platform CLR and a set of libraries for rich graphics, media and web programming. It is packaged as a Silverlight extension, which means that it is downloaded in addition to Silverlight on an as-needed basis.
DSL has a runtime and an SDK component. The runtime consists of two assemblies: Microsoft.Scripting.dll, and Microsoft.Scripting.Silverlight.dll. You'll also need the language assemblies, which are IronRuby.dll and IronRuby.Libraries.dll for Ruby and IronPython.dll and IronPython.Modules.dll for Python. The runtime component is a small additional download. Today, the IronRuby Silverlight runtime is just a 712KB download, which takes less than 5 seconds to download over modern broadband.
There is also an SDK component to DSL. The piece of software that you'll interact with the most is our local web server called Chiron. Created by John Messerly and Dmitry Robsman, it gives you a very nice interactive development experience using nothing more than your local file system. Let's take a look at Chiron.[1]
Update: Here's a link to the download of the Dynamic Silverlight SDK. The dsl.bat file can be found in the \bin directory of the SDK.
Open up a command prompt and ensure that Chiron is on your path:
Next, use our template generator to create a starter IronRuby Dynamic Silverlight project:
You'll see that we've generated a set of starter files for you. To run this app, type chiron /b on your command prompt:
This should bring up a browser window with the current directory's contents displayed:
If you click on the index.html file, you'll see that the browser fetches a number of files from the server, and displays our Hello, World message:
Chiron prints a list of files that the browser requested. The interesting file is app.xap. This is the way we package Silverlight components for distribution. It's just a ZIP file with a different name. To look at its contents, just point your browser at http://localhost:2060/app.xap, and you'll see that it contains:
This illustrates what Chiron does: it dynamically packages everything that you need to run your DSL application into the XAP and serves it up to your browser. The core DSL runtime pieces and the IronRuby assemblies are in the file, along with all of the other files that we generated in our starter application.
Binaries are listed in the AppManifest.xaml file that Chiron generated for you. You can see how Silverlight 2 boots DSL when you open the file:
The EntryPointAssembly is the DSL 'shim' that connects the DLR to Silverlight. The EntryPointType is the name of the type that Silverlight will run when it starts up. DynamicApplication derives from the Silverlight System.Windows.Application type, which defines the Startup event which is used to bootstrap the DLR.
Next, let's look at index.html. The <object> tag is all that is needed to startup Silverlight. No more external JS files for folks who are familiar with the older Silverlight 1.1 Alpha. The source attribute points to app.xap, and initParams are used to pass initialization data to your program.
Notice the reportErrors = errorLocation parameter. This points to a <div> element that we generated for you. We use this <div> to report runtime errors and their stack traces to you.
This element also has a dependency on error.css to define the formatting for the error messages. I think it's pretty cool that we were able to use the extensibility mechanisms of Silverlight to inject custom DLR error message formatting into Silverlight.
Next, lets look at app.rb:
You'll notice that we just run this script and inject our hello world message into the element tree defined by the XAML:
There's a little bit of magic in layout_root.message.text. The message XAML element is a child of the layout_root element. How does Ruby resolve these references? The answer is found in the required file, Silverlight.rb:
We're adding a method_missing method to the FrameworkElement class and the SilverlightApplication class. So when we try to retrieve layout_root, it delegates to the SilverlightApplication's implementation of method_missing. It dynamically invokes the target passed in m, which invokes the generic method_missing method in FrameworkElement. This gives a very natural feel for 'dotting through' a set of contained XAML elements, which is largely made possible by the Ruby programming language.
I hope this post gives you a feel for what Silverlight programming from IronRuby looks like. In the next part of this series, we'll look at how we can build a more sophisticated application that uses network stack, JSON, flickr, and some funky animation as well.
[1] The fine print:
There's still some work that we have left to do before we can push the sources and bits out. The code is baked, but packaging this stuff still takes some time. Check back here on Friday, the day of my talk at MIX (you are going, right?), and you'll have some bits that you can use to follow along with this tutorial.
Update: Here's a link to the download of the Dynamic Silverlight SDK.
You'll also need to have a working install of CRuby to run some of our utilities. I recommend the Ruby one-click installer for Windows. This will be fixed in the next release of IronRuby.













This is really cool. But it's hard to see it as a general solution if it doesn't run on Macs and Linux. I can see how it could work in a controlled environment, but in my case I can't depend on my users having PCs.
Is this a technology that is destined to stay on PCs only? Or is there a plan to move it to Mac and Linux as well?
Posted by: Timothy | March 05, 2008 at 08:04 AM
Really awesone, but I respect you man, using the poor power shell, instead of a more powerful shell (zsh, bash, etc.), with cygwin.
Posted by: Thibaut Assus | March 05, 2008 at 08:22 AM
@Timothy What makes you think this is PC only? IronRuby runs on Mono... John's very first paragraph uses the phrase "cross-platform"... ?
Posted by: Michael Letterle | March 05, 2008 at 09:09 AM
I guess it was the runtime dlls that made me think this is currently PC-only. Is that only for the development environment, or for deployment to browsers?
So this all works now on all platforms?
A screenshot in Camino instead of IE would have convinced me. :-)
Posted by: Timothy | March 05, 2008 at 09:15 AM
Mono uses the same PE format as the .NET CLR... so you can have dlls that run on both windows and linux/Mac ;)
Posted by: Michael Letterle | March 05, 2008 at 09:20 AM
Cool.
I wish you could of called it something besides DSL.
Posted by: bryanl | March 05, 2008 at 09:27 AM
@Timothy:
Dynamic Silverlight should just run on Moonlight modulo bugs / missing features. Debugging is something that I *know* works under IE and even that literally just got working reliably two days ago. So, that's life on the bleeding edge right now :)
We will work with Miguel's team to make sure that our stuff just runs on Moonlight. Chiron is a fully managed app that should just run under the desktop Mono stack.
Once we get our bits out at the end of this week, feel free to start opening bugs on our Mono support :)
Posted by: John Lam | March 05, 2008 at 09:44 AM
OK. If it works across browsers and platforms then it's worth a trial run for me.
Posted by: Timothy | March 05, 2008 at 10:18 AM
This is really cool stuff. I can't wait to get my hands on this on Friday!
Posted by: Christopher Bennage | March 05, 2008 at 08:13 PM
@Michael Letterle:
I'll second that. DSL is too confusing (see Domain Specific Languages).
Posted by: Shimon Amit | March 06, 2008 at 02:24 AM
+1. No more ambiguous TLA overloading, please!
"DSL" already has a meaning that plays into DLR and the whole concept of rolling your own language: Domain Specific Language
Posted by: Jason Follas | March 06, 2008 at 10:30 AM
John just called it "DSL" to put a simpler name for the integration between Silverlight and Dynamic Languages; there's no "real" name for it ... it's just all Silverlight.
Posted by: Jimmy Schementi | March 06, 2008 at 11:43 AM
Silverlight runs on Mac as well (http://www.microsoft.com/silverlight/resources/installationFiles.aspx?v=2.0), and Chiron runs on the Mac via Mono (http://www.mono-project.com/Main_Page).
As far as Linux support, the Mono team is working on an implementation of Silverlight called "Moonlight" (http://www.mono-project.com/Main_Page). Chiron also runs on Linux via Mono today.
Posted by: Jimmy Schementi | March 06, 2008 at 11:51 AM
John, your blog ate my links!
SL on Mac: http://www.microsoft.com/silverlight/resources/installationFiles.aspx?v=2.0
Mono: http://www.mono-project.com/Main_Page
Moonlight: http://www.mono-project.com/Moonlight
Posted by: Jimmy Schementi | March 06, 2008 at 11:57 AM
I vote for DWPF/E!
Actually DSlt.... then you could call it Dee-slight....
Bah, whatever... tell me the first release of Chiron will be called Chiron Beta Prime!
Is it just me or do I feel a slight Rails/WEBrick influence?
Posted by: Michael Letterle | March 06, 2008 at 12:05 PM
John, I mentioned your blog post on CodeProject: http://www.codeproject.com/script/Forums/View.aspx?fid=1159&msg=2453462
And one of the regulars there was outraged that your simple Hellow World app requires 700K. What would you say to that?
Posted by: Judah Himango | March 06, 2008 at 01:14 PM
In the case of Mac OS X and Linux, because httpd has already been installed, the Chiron.exe is unnecessary, I guess.
However, it is necessary to add some MIME setting to execute the app.xap .
See Chiron.exe.config in Silverlight SDK.
Posted by: KKI | March 06, 2008 at 07:25 PM
@Judah: Depending on how you configure your app, you can download the DSL dependencies (DLR + IronRuby) in a separate XAP that comes from one of our servers (this isn't working yet). So your XAP becomes something like 2K in that case. It will also be cached in your browser cache so it is a one-time download. Compare with any of the other binary redists of Ruby 1.8.6 for size ...
Posted by: John Lam | March 06, 2008 at 07:38 PM
Hi John,
>Depending on how you configure your app, you can download the DSL dependencies (DLR + IronRuby) in a separate XAP that comes from one of our servers (this isn't working yet).
>So your XAP becomes something like 2K in that case.
really?
It does not work because of the cross domain limitation, I guess.
Posted by: KKI | March 06, 2008 at 07:54 PM
Sorry, I just noticed the following URL.
http://msdn2.microsoft.com/en-us/library/cc197955(VS.95).aspx
Posted by: KKI | March 06, 2008 at 09:00 PM
Excellent, John, thanks for the info. I'll let the guy from CodeProject know.
Great job on getting IronRuby running in the browser! Really looking forward to experimenting with this.
Posted by: Judah Himango | March 07, 2008 at 07:53 AM
You are missing some steps in this presentation:
You jump from verifying that Chiron is in your path to invoking the template generator. Your screen shot shows a >> prompt. What CLI, shell, or app is generating this?
Where is the "dsl" template generator located?
I have seem several demos of the DLR console that show Ruby, Python, and JScript yet there does not seem to be a version available. Where can I find this?
Posted by: Tim | March 07, 2008 at 01:48 PM
In case anybody is wondering how to customize your powershell prompt to look like John's.
Create a directory WindowsPowerShell in My Documents and put a file profile1.ps in there with the following content:
function prompt
{
$host.ui.rawui.WindowTitle = $(get-location)
Write-Host ("+ " + $(get-location)) -foregroundcolor Yellow
return "ยป "
}
Posted by: Ivan Porto Carrero | March 08, 2008 at 12:45 AM
Looks very interesting. Unfortunately like Tim I'm stuck at:
"""
Next, use our template generator to create a starter IronRuby Dynamic Silverlight project:
"""
It would be useful to have some lights on this.
Thanks.
Posted by: francois | March 09, 2008 at 02:44 PM
@francois: hang in there. Will upload new templates later tonight.
Posted by: John Lam | March 09, 2008 at 03:59 PM
John,
I love what you have done and all that you are doing to help the Ruby community run on the MS platform (it can only help us in the end).
Being a big proponent of Ruby though, I'm wondering why you guys decided to latch onto the DSL acronym. I know that with all TLA's you have a chance of tripping over another one; however, with Ruby this seems more serious. One of the benefits of using Ruby (IMHO) is the ability to create DSL's (as in Domain Specific Languages) to not only aid development, but communication in code.
-Joe
Posted by: Joe OBrien | March 14, 2008 at 12:39 PM
I found a bug in either IronRuby or Silverlight, but am not sure where to put it, so here it goes:
When you require files from a subdir in app.rb, the use_xaml expression afterwards fails with the message 'XamlParseException: [Line: 0 Position: 0]
A workaround is to do another require in your current app-dir and then the error message suddenly dissapears. Very odd.
Posted by: Roel van der Hoorn | March 16, 2008 at 03:55 AM
Where can I find the dsl.rb template, since it doesn't seem to be included in the SDK or in the rubyforge repository?
Posted by: paul van brenk | March 18, 2008 at 02:37 AM
Ditto others about producing the template. I tried to figure it out but no go. DSL generator is a big ???. The concept of writing Ruby in Silverlight is what has made me interested in Silverlight. But so far its a dead end. Maybe I should just bite the bullet and learn Actionscript 3.
Posted by: ChessMess | March 24, 2008 at 07:35 AM
@{paul,chessmess} you can find the template in the DynamicSilverlight SDK at http://www.dynamicsilverlight.net
Posted by: John Lam | March 25, 2008 at 06:48 AM
I have having some trouble getting started. This is the problematic step: "Next, use our template generator to create a starter IronRuby Dynamic Silverlight project."
Where dose the dsl.rb file come from? How can I call this from a standard command prompt? I have Chiron and Ruby in my class path, not sure how to progress?
Thanks
Posted by: Daryn | March 26, 2008 at 05:29 AM
@daryn: the file lives in the \bin directory of the dynamicsilverlight SDK.
Posted by: John Lam | March 26, 2008 at 07:32 AM
@John: the file isn't there. I've tried downloading both the sdk from the location you specified in your post above, as from the codeplex site. In both cases, no dsl.rb in the \bin directory. All there is are a bunch of .dlls and chiron.exe (and a readme).
Any clues?
Posted by: Inferis | March 27, 2008 at 05:57 PM
All,
dsl.rb isn't shipped in DynamicSilverlight yet, it's a small ruby script that simply spits a template file structure; you can achieve the same file structure by just using the hello_world sample found in samples/ruby/hello_world as a starting point. I didn't tell John that it wouldn't ship in our package, so it's my fault =P
If you guys really want the script to do that for you, I can add it to dynamicsilverlight.zip for you.
Posted by: Jimmy Schementi | April 03, 2008 at 12:49 AM
@Roel: use_xaml is a helper method to load an associated xaml file for you automatically from a class which inherits from SilverlightApplication. use_xaml lives in Silverlight.rb, so feel free to change it ... and let me know what fixes you do to fix your bug.
I always use it from app.rb like this:
class App < SilverlightApplication
use_xaml
end
Posted by: Jimmy Schementi | April 03, 2008 at 12:52 AM
Hey Jimmy, that would maybe very cool if add the dsl.rb to the project, because it generates a little bit confusion at my side to get the example started.
thank a lot,
zazi
Posted by: zazi | April 29, 2008 at 02:55 AM
Will the dsl.rb included?
Posted by: Unni | May 04, 2008 at 07:16 PM
Question, is it only few controls are wired to work with Ironruby or all of WPF/E controls are enabled?
I tried some different panels and other controls like check box etc., aren't working. Just curious.
Posted by: Nair | May 05, 2008 at 02:25 PM
If you look at silverlight.rb, you'll see that I've only wrapped a few controls and properties.
Posted by: John Lam | May 06, 2008 at 11:17 AM
If you look at silverlight.rb, you'll see that I've only wrapped a few controls and properties.
Posted by: John Lam | May 06, 2008 at 11:17 AM
Tutorial - Develop Silverlight with Ruby on Mac OS X or Linux with the DLR SDK and a Text Editor (TextMate)
http://code.steelpotato.com/2008/07/tutorial-develop-silverlight-with-ruby.html
I updated the tutorial for Silverlight 2 Beta 2, and explain how to set up your system to follow John Lam's tutorial on Linux or OS X
Posted by: Sean Clark Hess | July 28, 2008 at 01:33 PM