How to force re-download of Silverlight XAP file
Asked Answered
S

3

7

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

Saxhorn answered 13/5, 2011 at 11:49 Comment(0)
I
8

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.

Idocrase answered 13/5, 2011 at 12:15 Comment(6)
I can't reach the "param" ("xapSource" in your example) object in codebehind, intellisense doesn't see it at all. Am I doing something wrong? What is it supposed to look like in designer.cs file? What is its exact type?Saxhorn
Alright, I got it to work. Param can't be reached programatically, it's necessary to use this solution: geekswithblogs.net/mbcrump/archive/2011/01/06/…Saxhorn
Yes you are doing something wrong. It should look like protected global::System.Web.UI.HtmlControls.HtmlGenericControl xapSource;. The exact type is System.Web.UI.HtmlControls.HtmlGenericControl .Idocrase
@VexXTtreme: "Param can't be reached programatically". Isn't true, I do it to tweak up the content of initParams. Often skilled silverlighters are little weak on older ASP.NET skills. You've got the runat="server" on the param right? it has the id "xapSource". Then you have yourself a HtmlGenericControl, you can manipulate its attributes via the Attributes collection.Idocrase
Yeah, you're right. The reason I couldn't reach it is because it wasn't in the designer.cs file, and I didn't know the exact type and couldn't add it manually so I assumed that it can't be reached at all.Saxhorn
@VexXtreme: I've just realised where potentially the confusion was coming from. The attribute to be modified is a "value" not "source". I've tweaked the answer accordingly, my bad.Idocrase
U
1

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.

Unrounded answered 12/4, 2012 at 14:54 Comment(0)
C
0

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).

Caban answered 13/5, 2011 at 12:3 Comment(1)
Technically no cache invalidation would take place, the browser has never seen that url before and therefore would not find it in the cache anyway.Idocrase

© 2022 - 2024 — McMap. All rights reserved.