How to force re-download of Silverlight XAP file

2019-02-09 16:59发布

问题:

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

回答1:

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.



回答2:

You can just append the current datetime to the xap and it will be seen as new each time, so no caching on the client.

value="ClientBin/SilverlightApplication1.xap?<%=DateTime.Now%>"

Hope this helps.



回答3:

The myApp is probably a web control that renders the Silverlight object in the page.

By setting its Source property the url that points to the XAP gets a parameter that is different from the previous version causing the browser to invalidate the cached xap(that had another value set for this parameter).