« IronRuby @ MIX 08 | Main | Dynamic Silverlight Part 2: Managed JScript and flickr »

March 05, 2008

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:

Chiron

Next, use our template generator to create a starter IronRuby Dynamic Silverlight project:

DSL

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:

StartChiron

This should bring up a browser window with the current directory's contents displayed:

Directory

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:

Hello IronRuby

Xap Appears

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:

app.xap

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:

AppManifest

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.

ObjectTag

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.

errorLocation

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:

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:

App.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:

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.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d834fc885d53ef00e55098547f8833

Listed below are links to weblogs that reference Dynamic Silverlight Part 1: Hello, World!:

Comments

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?

Really awesone, but I respect you man, using the poor power shell, instead of a more powerful shell (zsh, bash, etc.), with cygwin.

@Timothy What makes you think this is PC only? IronRuby runs on Mono... John's very first paragraph uses the phrase "cross-platform"... ?

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. :-)

Mono uses the same PE format as the .NET CLR... so you can have dlls that run on both windows and linux/Mac ;)

Cool.

I wish you could of called it something besides DSL.

@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 :)

OK. If it works across browsers and platforms then it's worth a trial run for me.

This is really cool stuff. I can't wait to get my hands on this on Friday!

@Michael Letterle:

I'll second that. DSL is too confusing (see Domain Specific Languages).

+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

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.

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.

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?

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?

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.

@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 ...

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.

Sorry, I just noticed the following URL.
http://msdn2.microsoft.com/en-us/library/cc197955(VS.95).aspx

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.

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?

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 "ยป "
}

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.

@francois: hang in there. Will upload new templates later tonight.

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

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.

Where can I find the dsl.rb template, since it doesn't seem to be included in the SDK or in the rubyforge repository?

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.

@{paul,chessmess} you can find the template in the DynamicSilverlight SDK at http://www.dynamicsilverlight.net

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

@daryn: the file lives in the \bin directory of the dynamicsilverlight SDK.

@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?

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.

@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

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

Will the dsl.rb included?

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.

If you look at silverlight.rb, you'll see that I've only wrapped a few controls and properties.

If you look at silverlight.rb, you'll see that I've only wrapped a few controls and properties.

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

I've been trying hard to get the 'Hello World' app to work, but no luck.

I get the icon, 'get Microsoft Silverlight'.

What gives. When I launch localhost:2060 on my application directory, I click index.html. I believe this should take me to my app. I copied all the dlls from /Program Files/Microsoft Silverlight/2* into my chiron/bin directory just to ensure that there were no problems with my path.

Sorry if this question is dumb, but after struggling for several days, I had to seek professional help :(

@"NO LIGHT IN SIGHT"

Did you download the latest release from http://codeplex.com/sdlsdk?
The other steps you describe are correct, but you definitely shouldn't copy files from Program Files, as they will never be loaded, only things in AppManifest.xaml are loaded.

If you're getting the "Get Microsoft Silverlight" image, please make sure you have the latest version of Silverlight installed.

If you are still having this problem, please open a defect on http://codeplex.com/sdlsdk, with your app attached.

In my opinion it already was discussed

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from John Lam. Make your own badge here.

May 2009

Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Blog powered by TypePad