Silverlight: Running standalone full trust applications

Introduction

I guess that it's time to write about it, after making a few people curious at the MVP summit in Seattle. This article is to be taken as a proof of concept, and (I hope) as a way to "motivate" Microsoft to integrate this ability into Silverlight.

As soon as I started playing with Silverlight, I saw the great potential that this technology has. As a RIA technology, it provides extended functionality over the web. As a WPF subset, it allows me to leverage the knowledge I already acquired in the past, and to reuse it for web applications. This in itself makes Silverlight a technology I *have to* learn.

But what if... what if we could use Silverlight to run lightweight .NET applications in standalone mode? A little like Adobe AIR is offering, but with the power of .NET instead of ActionScript. Imagine a small application, running on a small framework, installed in typically 10 seconds.

I posted an article on my site explaining the thought process and giving information about standalone Silverlight applications, their promises and their limitations. For those of you too impatient to read the full article :) you can download a demo and the source code directly from here.

The demo

To install the demo, follow the steps:

  • Download the Zip file.
  • Extract the Zip file to a folder on your hard drive.
  • On Windows: double-click the file "Start.hta".

This starts the MSHTA host, and displays the Silverlight application.

Silverlight application in MSHTA
Silverlight application in MSHTA
  • Enter a directory path in the first textbox, and then click on "Get list of files". This displays the corresponding list in the listbox.
  • Select a file. Its name is displayed in the second textbox.
  • You can now modify the file name and press on "Rename".
  • Enter any file path in the last textbox and press "Does file exist?". A corresponding message is displayed in the status bar.

This last functionality is also available in Mozilla-based browsers, for example Firefox. You can try it by setting Firefox to be your default browser, and then double-clicking the file "index.html" located in the same folder as "index.hta". This starts the application in "file" mode, and you get the same permissions. The other functionalities "Get list of files" and "Rename" are not implemented in Firefox and will raise an error.

Silverlight application in Firefox
Silverlight application in Firefox
Source code

As usual, the source code is available for download. It contains the following projects:

  • GalaSoft.SL.TestHta: This is the Silverlight application you want to run. It contains an "index.html" file, which should be the start page if you run the application in "F5" or "Ctrl-F5" mode in Studio. Visual Studio runs the HTML file in file mode, so you get a chance to debug the Silverlight code, but unfortunately it is not possible to debug the JavaScript code. When you build the project, a "index.hta" file is automatically created. This is exactly the same as "index.html", but with a different extension. Double-clicking this file in Windows Explorer will start the MSHTA host.
  • GalaSoft.SL.IO: This is a library defining a new DirectoryInfo and FileInfo classes. These classes mimic the actual System.IO.DirectoryInfo and System.IO.FileInfo classes. They use the class "GalaSoft.SL.IO.ScriptFacade" to channel the full-trust calls to the script code contained in the file "script\gslb.filesystemexplorer.js".
  • GalaSoft.SL.IO.Test: A Unit Test application for GalaSoft.SL.IO. Written using the Silverlight Unit Test framework and accessing JavaScript as described here. I use this to unit test all my IO classes and methods, including the corresponding JavaScript file.
    Note: The console application "GalaSoft.SL.IO.Test.Prepare" must be executed before the unit test is run.
  • GalaSoft.SL.IO.Test.Prepare: This console application is needed to create a few directories and files in the local drive in order to unit test the library GalaSoft.SL.IO. It requires write-permission to the folder c:\temp. Unfortunately, I don't think that Mock frameworks exist for JavaScript yet.

In normal use, the project GalaSoft.SL.TestHta should be chosen as startup project, and its file "index.html" as startup page. You can modify the application code. If you change the HTML code, the changes will automatically be copied to "index.hta" when you build the application.

Components involved in a standalone Silverlight application
Components involved in a standalone Silverlight application

http://www.galasoft.ch

Print | posted on Thursday, April 24, 2008 2:09 PM

Feedback

# re: Silverlight: Running standalone full trust applications

left by Adam Hill at 4/25/2008 10:57 PM Gravatar
Cool example. This is a blast from the past for me.

Back in the late 90's I built computer based training apps that could run locally or across the web using HTA's, XMLHttpRequest, Direct Animation (anyone remember that??) and VRML.

Thanks for showing me what was old, is new once new again. :)

# re: Silverlight: Running standalone full trust applications

left by Laurent at 4/25/2008 11:40 PM Gravatar
Adam,

I think it's what I prefer in that sample, the mix of very old and very new technology. This sample really talks to my "pragmatic programmer" sense, and to my engineer taste for "making things work". Happy you like it.

Laurent

# re: Silverlight: Running standalone full trust applications

left by Ratchetr at 4/26/2008 6:13 AM Gravatar
Laurent,

Thanks for providing me with an interesting Friday night programming challenge (Yeah, I really should get a life).

I'm more or less a die hard MS developer, 15+ years now. But I've been picking up Flex the last couple months because it's the only real solution to some requirements I have.

Just for kicks, I decided to try rewriting your app in AIR. My first AIR app...Yahoo(not affiliated with Microsoft yet, but...)!!!

The results of my test:
90 minutes, 33 lines of code (mxml with embedded ActionScript), I had a running AIR app with the same core functionality. To be fair, I didn't add exception handling, and my UI was default drag and drop stuff. But you have to admit, 33 LOC to get the UI and functionality in place, in under 2 man hours by an AIR rookie...tell me again why Silverlight is relevant??? ;-)

After I wrote my solution, I looked at your source code. Your Page.xaml.cs is reasonably similar to my Test.mxml file. Your error handling is better. But then you have all this other 'stuff' I didn't need. DirectoryInfo.cs. gslb.FileSystemExplorer.js. Yuck...that one has
references to Mozilla, explorer, ActiveX...Um...I don't want to worry about all that stuff :-)

I don't have a Mac, so I couldn't test my solution there, but I did test it on Ubuntu with the AIR Alpha, and...yeah, it just worked. I can rename files in /tmp just as well as c:\temp.
Not bad for 33 lines of code.
Can your solution do that?

You raised an interesting question in your original post:
"Does Microsoft really want to allow small .NET applications to run everywhere? What does it mean for Windows?"

They probably don't want to allow that. But what if someone else does? And what if someone else can deliver? What does THAT mean for Windows???



# re: Silverlight: Running standalone full trust applications

left by Laurent at 4/26/2008 8:04 AM Gravatar
@Ratchetr

You are absolutely right, and you make excellent points. Exactly the kind of discussion I was hoping to stir with this article. Thank you!!

I have answers , but I am going out for the day (yeah I do have a life outside of the PC, apparently :) so I will write more tonight.

Greetings!
Laurent

# re: Silverlight: Running standalone full trust applications

left by Laurent at 4/26/2008 10:48 PM Gravatar
OK, here is what I wanted to say about Ratchetr's post.

First, this is only a proof of concept. As I wrote, it's not something I would use in a productive project. This would only be viable if the IO namespace (what I simulate in my example with GalaSoft.IO, the ScriptFacade, the script file and the FileSystemObject) was working natively from Silverlight. That's not the case yet, but if it is eventually integrated in the framework, you wouldn't count that as lines of code. So to be fair, we need to count only the code included in Page.xaml.cs.

Also, we could only consider "standalone Silverlight" viable if it was made available on all the platforms on which "web Silverlight" runs. Honestly, I don't need that on as many platforms as Flash. If we get something like that to work on Windows, Mac and whatever flavour of Linux Moonlight runs on, I'd be delighted.

Even then, you might prefer to work with AIR instead of "standalone Silverlight". The great thing is that you can, and I love that diversity. I never did the step of learning AIR, but it does look very interesting (I actually use an AIR application, Twhirl, and it works fine). I do think that "standalone Silverlight" development would be more comfortable *for me* than AIR development, because I am fluent in .NET, I know WPF reasonably well so I can enjoy the continuum, I like Visual Studio a lot, etc... These reasons are personal, and I am not pretending that they are true for everyone. If "standalone Silverlight" is made available one day, I will most probably use it. In the mean time, I will maybe play with my self-made framework from time to time, because it's fun. But then again, I have a weird definition of fun :)

So yes, your points are totally valid, at least for that very simple application. For more complex applications, one could argue that .NET (even a subset) may be more suited than ActionScript, but again, I agree that AIR is a cool framework, and I totally encourage you to use it if it solves your requirements!!

As for the more political question of allowing something like standalone Silverlight to happen... I am really not qualified to answer that :) One thing is sure though: The next couple of years are going to be totally fascinating (well, for geeks like us anyway).

Thanks for your comments!
Laurent

# re: Silverlight: Running standalone full trust applications

left by Marlon Grech at 4/27/2008 5:03 PM Gravatar
Hey dude,

This is so cool! You're the man!

Regards

# re: Silverlight: Running standalone full trust applications

left by Michael Sync at 4/27/2008 6:25 PM Gravatar
Hi,

What is the differences between your application and WPF browser application?

# re: Silverlight: Running standalone full trust applications

left by Laurent at 4/27/2008 7:58 PM Gravatar
Hi Michael,

I almost want to say that this application is the exact contrary of an XBAP:

An XBAP is a WPF application running embedded in a web browser, often in partial trust.

This is a Silverlight application, running as a standalone application, and accessing the file system.

XBAPs and Silverlight are really two different beasts. The biggest advantage of my proposal if you have nothing install, the Silverlight installer is a mere 5 MB versus the full .NET framework.

For other differences between Silverlight and XBAPs, I recommend you to look for references on MSDN.

Greetings,
Laurent

# re: Silverlight: Running standalone full trust applications

left by Ratchetr at 4/28/2008 3:52 AM Gravatar
Laurent,

Your conclusion is spot on: "The next couple of years are going to be totally fascinating"
Microsoft and Adobe have made the RIA landscape very interesting. Adobe has the lead right now (IMHO). But Microsoft has the superior technology, and they have deeper pockets.

Adobe has the lead: 5 months ago I was given a requirement to produce an RIA replacement for an existing SSRS based web reporting solution (that, um...sucks). I wanted Silverlight to be the solution, for the same reasons you like Silverlight...leverage your existing knowledge. But it was the 1.x days. I needed charts and datagrids and web service databinding and... So I went with Flex. We did some trade show demos last week, the feedback was very positive. Should be in field trial shortly. But if I was starting from scratch today, Silverlight would warrant a look. 2.0 starts to deliver.

Microsoft has the superior technology: I haven't decided yet if xaml is any better/worser than mxml, but C# (or any other CLR based language) is clearly superior to ActionScript. My kingdom for generics in AS4!!!!(Anyone from Adobe reading this?).

I have a new requirement to meet 6 months from now: A collection of desktop 'widgets' that show a snippet of info, but hide in the tray most of the time. They have to work on Windows and Mac, and, yeah, Linux too. So here I am again, looking at a solution from Microsoft that doesn't even come close, and a solution from Adobe that works today (AIR).

Your blog is the only hint at a suggestion to where Silverlight should go in the future iff Microsoft thinks it's a priority. And they have a huge set of problems to solve to get there. (Security is a biggie. As is cross-platform support.)

I've sort of been boloxed into a corner again. I want a .NET solution, but MS can't deliver.
At least not yet.


# re: Silverlight: Running standalone full trust applications

left by Laurent at 8/16/2008 6:01 PM Gravatar
Links corrected, sorry

# re: Silverlight: Running standalone full trust applications

left by Lyne at 11/30/2008 11:53 PM Gravatar
Hello..

It asks me to download the silverlight beta version and I am using the RTM..

Could it be possible to add a version that would work with RTM too?

# re: Silverlight: Running standalone full trust applications

left by Laurent at 12/1/2008 8:17 AM Gravatar
Hi Lyne,

I need to convert all my samples to SL2 RTM, you are right. I didn't have time yet because I am starting working on my new job, and this took all my time. But this is definitely on my list.

Cheers,
Laurent
Comments have been closed on this topic.