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
param = "<param name=\"source\" value=\"" + orgSourceValue + "\" />";
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") + "\" />";
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.
Hope this helps someone out there.
4 thoughts on “IE9 and Silverlight caching problems”
Thanks for your article! BTW, I suppose that “yyyymmddhhmmss” must be changed to “yyyyMMddhhmmss”, otherwise “minutes” are used instead of “month”.
You’re right, thanks!
Is there a way to do this in straight html? Solution above only seems to work when you are using the aspx file,