Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight: how to force browser to download updated client version?

My Silverlight (4.0) app (hosted by ASP.NET website) uses 4 projects, they all use one file with assembly version:

[assembly: AssemblyVersion("1.0.*")]

The version of currently displayed application is 1.0.3842.38865, but the newer one (1.0.3854.42448) is uploaded to server recently.

The problem is that browser doesn't load new Silverlight application after it's deployment to server.

Here is a HTML-code that is used for "rendering" of silverlight-html-loader (not sure if it a correct name):

<div id="silverlightControlHost" style="height:950px"> 
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> 
  <param name="source" value="/ClientBin/VfmElitaSilverlightClientApplication.xap"/> 
  <param name="onError" value="onSilverlightError" /> 
  <param name="background" value="white" /> 
  <param name="initParams" value="adr=squad,team=811,match=3217203" /> 
  <param name="minRuntimeVersion" value="3.0.40624.0" /> 
  <param name="autoUpgrade" value="true" /> 
  <a href="http://go.microsoft.com/fwlink/?LinkID=149156&v=3.0.40624.0" style="text-decoration:none"> 
      <img src="http://go.microsoft.com/fwlink/?LinkId=108181" alt="Get Microsoft Silverlight" style="border-style:none"/> 
  </a> 
</object> 
<iframe id="_sl_historyFrame" style="visibility:hidden;height:0px;width:0px;border:0px"></iframe> 

I've tried to add a parameter to the "source" parameter of the object that contains time of last XAP-file modification:

  <param name="source" value="/ClientBin/VfmElitaSilverlightClientApplication.xap?Ver=2010072243523AM"/>

That caused an error of Silverlight application loading:

Unhandled Error in Silverlight Application Code: 2103 Category: InitializeError Message: Invalid or malformed application: Check manifest

Could you please advise how to force browser to get new application from server (without browser cache manipulating, I would like to keep browser caching option)?

Thank you very much!

P.S. It is necessary to add that silverlight application works (uploaded and launched) fine on my localhost without any dancing with parameters. Only when I upload it to web site - it is not reloaded by browser. And adding additional parameters to the xap-file path - doesn't work on localhost.

like image 343
Budda Avatar asked Jul 22 '10 05:07

Budda


4 Answers

I'm still testing, but so far, it appears that modifying the AssmeblyFileVersion forces the browser to download the latest xap file. Using Silverlight 4, I tried many of the other solutions offered and couldn't get them to work or they were undesirable (like no caching at all). Now I'm incrementing the file version, and it seems to grab the latest xap every time.

[assembly: AssemblyFileVersion("1.0.0.1234")]
like image 23
S_Niles Avatar answered Oct 19 '22 17:10

S_Niles


What we use currently is the following which gets the last write time of the .xap-file and appends it to the source-param:

<object ... >
        <%
            var source = "ClientBin/App.xap";
            string param;
            if (System.Diagnostics.Debugger.IsAttached)
                param = string.Format("<param name=\"source\" value=\"{0}\" />", source);
            else
            {
                var path = HttpContext.Current.Server.MapPath(string.Empty) + "\\" + source;
                var xapCreatedAt = System.IO.File.GetLastWriteTime(path);
                param = string.Format("<param name=\"source\" value=\"{0}?version={1}\" />",
                    source,
                    xapCreatedAt.ToString("yyyyMMddTHHmmssfff"));
            }
            Response.Write(param);
        %>
        <param ...
like image 140
JHiller Avatar answered Oct 19 '22 18:10

JHiller


This should work as expected, maybe it has something to do with how you are appending the param. Try removing the Ver= portion:

<param name="source"
    value="/ClientBin/VfmElitaSilverlightClientApplication.xap?2010072243523AM"/> 

I have used this method in the past and it is the best way to get around any client side caching.

If you want it always to refresh and never cache you can just add the current DateTime to the end which will always be unique as well. Not sure when you would want to do this in a real world scenario but it is great for testing to ensure you never have a cached version running. Eg:

<param name="source"
    value="/ClientBin/VfmElitaSilverlightClientApplication.xap?<%= DateTime.Now.Ticks.ToString() "/>

If this isn't working, just remove it all together with no addition to the end and see if it loads. I have a feeling that the error is something else as it doesn't really apply to the location of the xap file.

like image 2
Kelsey Avatar answered Oct 19 '22 17:10

Kelsey


The correct approach to managing the browsers cache is to tell it what you expect of it via the approproiate http response headers sent by the server.

In IIS manager specify that the content of the ClientBin folder be expired immediately.

Note that ths doesn't mean that the Xap wil be downloaded on every request, just that the browser should check that its cached copy is up-to-date.

like image 1
AnthonyWJones Avatar answered Oct 19 '22 16:10

AnthonyWJones