Navigate back home
Creative Commons License
GalaSoft by Laurent Bugnion is licensed under a Creative Commons Attribution 3.0 Unported License
GalaSoft Laurent Bugnion
Standalone Silverlight 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.

Why would you want a standalone Silverlight application?

There are many reasons why one would want to create a standalone, full-trust Silverlight application. To me, the most compelling ones are:

Note that the full .NET setup experience is going to get better soon, with a "client only" setup. Many .NET applications don't need server-side libraries, so giving the user a way to install only client-side libraries makes the whole .NET experience better. This "client only" setup is coming in .NET 3.5 SP1 and should be around 25 MB in size.

Why don't we have standalone Silverlight applications already?

Note: I don't have insider information here. This is only based on discussions I had with other Silverlight specialists and based on my own judgement.

Disclaimer

At this time, Silverlight is *not* a light WPF application. It was not inteneded to run as standalone. The technique demonstrated here does not represent what Silverlight was made for, which is running in the web browser.

So how does it work?

This application is a proof of concept more than anything else. It relies only on documented Microsoft technologies, so there are no dirty tricks at work here. It is based on a few facts that I happen to have learned in past projects:

These facts alone are sufficient to create a standalone Silverlight application using JavaScript as a fully trusted API to perform operations that would otherwise be forbidden. To start the HTML file with elevated permissions, in other words to run it in "file" mode, you must double-click the HTML file in Windows Explorer (or the equivalent on other platforms).

Running a HTML file in "file" mode is not much different for the user than running it in HTTP mode. The location bar, back and forward buttons and all the other browser controls are visible. To give the user a better experience, there is a better, though lesser known way:

Limitations

The HTA experience has limitations though:

Why do we need JavaScript at all?

For the moment, Silverlight doesn't support any full trust at all. Some classes exist, which could require elevated permissions (the whole System.IO comes to mind). However, if you try to create a DirectoryInfo or a FileInfo, you get a security exception, even if the HTML/JavaScript host runs in full trust. These exceptions are likely hard coded for the moment. FileInfo and DirectoryInfo are needed for the access to the IsolatedStorage to work, but as soon as you want to do something on the hard drive, you get issues.

JavaScript allows these operations to occur in full trust, however. This is why, as long as Silverlight won't allow such functionality to work, using JavaScript from Silverlight is the only alternative.

The following image shows the layers involved in the standalone Silverlight application.

Components involved in a standalone Silverlight application
Components involved in a standalone Silverlight application
What is "file" mode and "http" mode

An HTML page can be run in "file" mode (or "local" mode) by double-clicking it to start it. In "file" mode, the URL displayed in the location bar starts with the "file:" protocol. In IE, you will see a Windows-like path, for example "c:\temp\test.html". This mode offers elevated permissions to the JavaScript code, running in full trust.

In "http" mode, the URL starts with the "http:" protocol, and the elevated permission is not available. The application runs in partial trust.

Does it run on other platforms?

MSHTA is for Windows only, so the HTML Application host will not run on other platforms (Mac, Linux) at all.

However, the ability that JavaScript has to access the underlying file system is also available for other platforms.

Some time ago, I published a "proof of concept" JavaScript class allowing file system operations in JavaScript, and auto-detecting which are possible on the current platform. This file contains an implementation for multiple platforms of certain file system operations. The elevated permission can be reached by running the HTML application using this JavaScript file in "file" mode. I am reusing some of this code here, and implemented only one method for Mozilla-based browsers: "fileExists". This method returns true if a given file exists, and false if it cannot be found.

However, programming for the "components" based platform is a real pain. The syntax is overly complex and not well documented. If anyone is willing to implement additional operations for Mozilla-based platforms, don't hesitate. On my side, I think I will pass for now :) This is why other operations will fail.

The demo

To install the demo, follow the steps:

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

Silverlight application in MSHTA
Silverlight application in MSHTA

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:

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.

Ideas for further development

As I wrote, this application is more a "proof of concept" than anything else. I have a few ideas for further development that I will pursue if time allows, and I also have a couple of ideas of projects in which I could use this kind of lightweight .NET application.

If you have any idea how we could make this better, don't hesitate to write to me. If enough interest is available, we can even start an open-source project at Codeplex or something.

Date Version Description
23.04.2008 V1.0.0 First published version