Uncategorized

IE9 and Silverlight caching problems

I ran into a problem with customers not able to retrieve a new copy of a silverlight app using IE. I took over a user’s session remotely to witness this myself. I pressed F5 and Ctrl-F5 (hard refresh) and while the HTML page hard-refreshed, the XAP file refused to load until I deleted the customer’s IE cache and reloaded it. Why IE refused to honor the hard refresh, I will never know; but one thing is for certain, and that is you can NEVER trust the caching policies of the browser completely.

I found this link http://codeblog.larsholm.net/2010/02/avoid-incorrect-caching-of-silverlight-xap-file/ describing how to fix this issue. In the OBJECT tag that hosts the SL app in your HTML page, you append to the end of the XAP file url the last file modification date/time. This way, the XAP file will only reload if the file changes. I had to make some modifications to the code I got from the aforementioned URL to make it work properly.

So, instead of:

<param name="source" value="ClientBin/SilverlightApp.xap"/>

You want to put in its place:

<%
string orgSourceValue = @"~/ClientBin/SilverlightApplication.xap"; // path to and name of your XAP file
string param;
if (System.Diagnostics.Debugger.IsAttached)
{
param = "<param name=\"source\" value=\"" + orgSourceValue + "\" />";
}
else
{
string xappath = HttpContext.Current.Server.MapPath(orgSourceValue);
DateTime xapCreationDate = System.IO.File.GetLastWriteTime(xappath);
param = "<param name=\"source\" value=\"" + orgSourceValue.TrimStart('~') + "?ignore=" + xapCreationDate.ToString("yyyymmddhhmmss") + "\" />";
}
Response.Write(param);
%>

This way, if you are debugging your application, the path remains the same, but if there is no debugger attached, then the file is cached as long as the file modification date does not change.

I made the following changes from the original poster’s code:

  • The output of the time was a simple .ToString() which did not form a valid URL. This has been changed into more of a timestamp, where all non-numeric values were removed.
  • How the path to the XAP file was stored has been changed. Since we’re working from the base dir out, I used the .Net style of server paths, prepending the tilde (~) and then when writing it out, removing the tilde from the start of the path.
I will also add that the “ignore?” portion can really be anything you want it to be. As long as the URL does not change, the application will be cached by browsers that are set up to support caching. Once you upload a new copy of your XAP file, the file modification date will change, and then the URL changes, thus forcing browsers to get a new copy of your app.

Hope this helps someone out there.

4 thoughts on “IE9 and Silverlight caching problems

  1. Thanks for your article! BTW, I suppose that “yyyymmddhhmmss” must be changed to “yyyyMMddhhmmss”, otherwise “minutes” are used instead of “month”.

  2. Is there a way to do this in straight html? Solution above only seems to work when you are using the aspx file,

    1. You would have to use Javascript. All you’re doing is getting either a random number or constructing a number from the current date and time, and appending that to the end of the URL. You wouldn’t use any of the tildes and you wouldn’t need the logic that determines whether it’s in a debugger or not – strip all that out and you should be good to go.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s