How to force re-download of Silverlight XAP file

browser-cachesilverlightversioningxap

I'm trying to figure out how to force the browser to re-download a .xap file if the new version is available and yet the old one is still cached in the browser.

I've seen the other thread:
How do you force Firefox to not cache or re-download a Silverlight XAP file?

The best solution seems to be:

protected void Page_Load(object sender, EventArgs e)
{
    var versionNumber = Assembly.GetExecutingAssembly().GetName().Version.ToString();
    this.myApp.Source += "?" + versionNumber;
}

However, I don't get the this.myApp part. What kind of object is that? I'm sorry for re-opening this, but I wish people would post complete solutions.

Thanks

Best Answer

What your looking at is code based on the asp:Silverlight web server control but that control was discontinued from Silverlight 3 onwards.

Now we either use the object tag directly or knock up our own server controls to render our preference of object tag.

As an object tag it would look something like this:-

<form id="form1" runat="server" style="height:100%">
<div id="silverlightControlHost">
    <object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%">
      <param id="xapSource" runat="server" name="source" value="ClientBin/SilverlightApplication1.xap"/>
      <param name="onError" value="onSilverlightError" />
      <param name="background" value="white" />
      <param name="minRuntimeVersion" value="4.0.50303.0" />
      <param name="autoUpgrade" value="true" />
      <param name="initParams" id="initParams" runat="server" />
      <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=4.0.50303.0" style="text-decoration:none">
          <img src="http://go.microsoft.com/fwlink/?LinkId=161376" alt="Get Microsoft Silverlight" style="border-style:none"/>
      </a>
    </object><iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe></div>
</form>

Note the id and runat="server" on the source param. With that in place the page load could look something like this:-

protected void Page_Load(object sender, EventArgs e)
{
    string xapPhysicalPath = Server.MapPath(xapSource.Attributes["value"]);
    DateTime lastWrite = System.IO.File.GetLastWriteTime(xapPhysicalPath);
    xapSource.Attributes["value"] = xapSource.Attributes["value"] + "?" + lastWrite.ToString("yyyyMMddThh:mm:ss");

}

This would ensure the url used for the source would always change when the xap has changed. The original code you've come across is flawed in that it is still possible for the xap to change without the entirely unconnected assembly version number changing.