On a project that I was working on lately I had to use Silverlight OOB (Out Of the Browser) and the xap file was actually not hosted in IIS but rather in a custom application (read either console app or Windows Services for the scope of this post).
The architectural approach is:
On the server side we have a console application running that host a WCF REST Service which will deliver, using WebGet, the files needed to run the Silverlight application. In other words the WCF service takes the place of IIS.
This is so that the user easily go to a web page then install the Silverlight application locally.
Now let’s see the implementation for this approach:
First the Service interface:
And the implementation:
Now lets explain the code a little bit:
1. The user open the web url for this application using a web browser the WCF will intercept the request and get return the html file stored locally see GetHtml method.
2. The browser parse the html and loads the Silverlight.js and the xap file.
3. The clientaccesspolicy.xml is provided in case there are other WCF services hosted under this url.
Now let’s talk about the OOB update of the application:
The process of upgrading a Silverlight application is described here:
Silverlight 3 Out-of-browser Update Model by Tim Heuer (the same applies for Silverlight 4 in case you are wondering)
As described in Tim’s post the Silverlight PlugIn send a request to the original web location of the Silverlight application using a special header, If-Modified-Since which IIS interprets it and respond correctly to it. However our WCF service will not know what to do with it by default so it is up to us to correctly handle this (see GetXap method).
The main idea here is that each time when the Silverlight app is either installed or upgraded some metadata is stored locally including a timestamp of the last update. when calling Application.Current.CheckAndDownloadUpdateAsync(); the plugin will check if the file was modified after the last time stamp and the expectation is that the server will respond with a status code of 304 Not Modified and an empty body when there was no update. However when the file is newer then regular response is expected including the xap file and a status code of 200 OK.
There were plenty of posts out there explaining how to host Silverlight inside WCF but none talked about the issue with the update. Hope this post will be helpful to people who will wonder why their application always reports update available even when there is none.