Silverlight and IronRuby – a match made in heaven

by DotNetNerd 25. April 2010 10:31

A couple of months ago I was starting to read about IronRuby, and while thinking about what my first little pet project should be I saw a tweet from a guy who was enjoying the combination with Silverlight. I gave the idea some thought and liked the idea of using a dynamic language to make a rich internet application. So after reading IronRuby UnleashedI started out doing a version 2 of my dotnetnerd.dk site – which is just a little toy selfpromotion site.

Getting started was pretty straight forward because with IronRuby there is a small webserver called Chiron, which can make a project template. All you have to do is open a cmd, and go to the library where IronRuby is installed and type script\sl.bat ruby <projectpath>

Armed with my starter template I began playing around and got my layout in place. I also tried using some 3rd party components by referencing some dll’s and all that basic stuff. Most of it went smoothly, but I did run into an assembly (Flickr.net) that threw an exception when used with Silverlight/IronRuby. Using it from a console project in IronRuby worked fine, so I quickly decided to just go directly against the flickr api using the .NET WebClient class. Running the site from chiron was as easy as calling script\chr.bat /d:<projectpath> /b:index.html

When I had the first couple of pages ready and had written a picturegallery showing the last 5 pictures I have uploaded to flickr I wanted to get the site deployed, so I would no longer depend on chiron. From my book and by looking at blogs it seemed to be smooth sailing, because chiron has a function that makes a .xap file which is all you need. To my surprise when I referenced the .xap file from the html file in my my project it looked like it loaded, but then just stopped at 100% without showing my actual site. I felt pretty stuck, because I had no exception or anything to go on, and my site ran fine when I was using chiron.

I then wrote an email to “Iron” Shay Friendman, who wrote the book that I was using as my inspiration. I thought it was worth a shot, and that I could not be the only one with that problem. Later that day he wrote back, and (as the nice guy he is) told me that he did not know the solution off the top of his head but he would look at it as soon as he had a couple of available hours. A few days later he had found a solution, and it turnes out a few things need to be done differently when running it outside of chiron. So this is basically what I want to share with this post :)

What you need to do is:

1) If your .rb files contain references like “app.xaml” it should be changed to app\app.xaml – in other words the references should be from the root of the solution and not from the app folder where the file is located.

2) Make the .xap file using the command script\chr /d:<projectpath> /z:<projectpath>\app.xap

3) In the index.html file where the .xap file is referenced find the like starting with <param name="initParams".
Change its value attribute to "start=app\app.rb,reportErrors=errorLocation".

And easy as 1-2-3, your site should work when running the index.html file.

Shortly put it really has been a fun project, and I really like the IronRuby and Silverlight combination, so it is definately not my last project where I will combine the two.

ironrubylogo

Comments (2) -

Jimmy Schementi
Jimmy Schementi United States
4/27/2010 8:29:07 AM #

Good to hear you're happy with the Silverlight integration in IronRuby. Sounds like you had two issues; (1) loading assemblies and (2) changing paths when deploying. Good news is there are actually not issues at all ... assemblies work in Silverlight from IronRuby as long as they are built for Silverlight, and you don't need to change any paths as long as you give Chiron the correct arguments.

I'll address your specific points:

I did run into an assembly (Flickr.net) that threw an exception when used with Silverlight/IronRuby. Using it from a console project in IronRuby worked fine ...

Yes, assemblies need to be built for Silverlight to work in Silverlight; since you said that Flickr.NET worked in the cmdline, then that's why it didn't work in Silverlight ... it was built for the desktop .NET framework. If you got a build of Flickr.NET for Silverlight, or got Flickr.NET's source code and recompiled it for Silverlight, it would be usable.

Using the Flickr REST APIs are easy enough, and I do this in a sample I talk about in this article: www.rubyinside.com/...ht-ruby-in-browser-3192.html

1) If your .rb files contain references like “app.xaml” it should be changed to app\app.xaml – in other words the references should be from the root of the solution and not from the app folder where the file is located.

Actually, the purpose of the "app" folder is that it is suppose to be turned into the "app.xap" folder, so paths absolutely shouldn't change ... that would be evil if I required you to change paths to deploy your app =)

2) Make the .xap file using the command script\chr /d:<projectpath> /z:<projectpath>\app.xap

Using that command is why you had to change the paths; the command should be script\chr /d:<projectpath>\app /z:<projectpath>\app.xap ... then you won't need to change paths. Maybe the template should have app.xap as the actual folder name, so no confusion can be had...

3) In the index.html file where the .xap file is referenced find the like starting with <param name="initParams".
Change its value attribute to "start=app\app.rb,reportErrors=errorLocation".


Again, if you use the correct flags for chiron, changing this isn't necessary.


If you like writing Silverlight apps in IronRuby, you should really look at http://ironruby.net/browser/ , as it shows an even simpler way of doing so ... by using ruby HTML script-tags. Also that article I mentioned earlier has a bunch of examples: www.rubyinside.com/...ht-ruby-in-browser-3192.html

And very cool new homepage ( http://dotnetnerd.dk/v2/ ) ... it's awesome that you wrote it in IronRuby! Feel free to email me if you have any questions, as it'd be great to see it on your main site.

DotNetNerd
DotNetNerd Denmark
4/27/2010 9:05:24 AM #

Thanks a lot Jimmy! I'll be sure to grab hold of you if I run into further questions. And great work by the way - it really has been fun to work with, and over all a pleasure how easy it has been to get started. Even though I had these small issues I needed to solve, it has been easier than I would ever expect to get started since it was not even released yet back when I started.

Sounds like a good idea to make the template a little more clear. Of course it makes perfect sence - it was just difficult to see exactly how to do the deployment.

I have taken a look at running it in html script tags actually. It looks really cool, but for this I wanted it to be deployed as a xap file for convenience.

Who am I?

My name is Christian Holm Diget, and I work as an independent consultant, in Denmark, where I write code, give advice on architecture and help with training. On the side I get to do a bit of speaking and help with miscellaneous community events.

Some of my primary focus areas are code quality, programming languages and using new technologies to provide value.

Microsoft Certified Professional Developer

Microsoft Most Valuable Professional

Month List

bedava tv izle