How do I compile .py files to a DLL for linking into silverlight

May 18, 2009 at 12:44 PM

We are developing quite a big application in Silverlight using ironpython. And the initial loading of Silverlight is very slow. We think its due to number of modules getting compiled at the time of loading. So I'm trying to compile the code into dll and use it in I tried clr.CompileModules("foo.dll", "") to make the dll but when I run using Chiron I'm getting this error

Could not load type 'Microsoft.Runtime.CompilerServices.StrongBox`1' from assembly 'Microsoft.Scripting.Core, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35'

I'm not using any SL specific libraries in the modules except Can anyone suggest a solution for my problem. If any other idea to improve the performance is highly appreciated.



May 18, 2009 at 3:40 PM

My understanding from asking a similar question recently is this:

Standard .Net/Mono and Silverlight differ in their core runtime assemblies as Silverlight is a more stripped down version.  Anything you compile from "userspace" IronPython is going to be linked against .Net/Mono runtimes.

Thus, they won't run in Silverlight.  The error you're getting about cannot load type is because it's looking for the .Net CLR Assembly Microsoft.Scripting.Core which isn't found in Silverlight/Chiron's runtime.

The answer is that you need to compile your assemblies for Silverlight.

From talking to jimmysch, the core developer of SDL-SDK, I got some leads and poked around.  IronPython does NOT currently have the right internal code to do what needs to be done:

Compile the Python module to an Assembly, *SAVE THE ASSEMBLY TO ISOLATED STORAGE*, and download to the Browser.

Yes, the above seems... overboard but there is no bootstrapping available to compile against a different runtime set in IronPython.

The above is doable, I am working on adding a few additional code paths for Assembly compilation in IronPython, and building essentially a "Browser based compiler".

It's not ideal, but the best solution.  I haven't had a lot of time to work on it in my end due to a similar issue as you (Working on developing a big application in IronPython which is eating up all my time, and the compiler is a "before it ships" issue rather than a now issue), but if you look at ; jimmysch and I were going back and forth about it.

Really all that needs to be done is adding a Assembly compilation path within IronPython to return a byte[] to userspace [currently it only allows you to save to the filesystem and Silverlight can't access normal FS anyway].

Another option I'm looking at is that Boo appears to have a command line compiler that's capable of spitting out precompiled Silverlight assemblies.  Since Boo is also a DLR language, I'm mildly curious about whether I can steal some of it's code and build a command line Silverlight compiler for Iron Python (But don't wait on me or you'll probably die of boredom or old age).

I will update this thread and the other if I find time to make things work.  Would appreciate if anyone who figures it out would do the same ...


May 19, 2009 at 5:36 AM

Thanks for your information. I'll also look into it.

Jun 16, 2009 at 5:55 PM

I've started working on this the last couple of days.

I'm not certain yet if I'll need to produce an IronPython patch or I can make my stuff operate independently....

I haven't checked any code in yet, but I did setup a google code project for the work:

I'm aiming to create at the least to start with either a linkable DLL that you can invoke from a running sdl-sdk app to compile the code in the XAP, or a standalone Silverlight App that can convert an uploaded XAP to dll.

I am also looking into implementing a command line compilation with a --nostdlib option similar to Boo's which will allow linking specifically against the Silverlight / SDL-SDK assemblies.