Silverlight:如何强制浏览器下载更新的客户端版本?

时间:2022-10-22 14:44:01

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

我的Silverlight(4.0)应用程序(由ASP.NET网站托管)使用4个项目,它们都使用一个带有程序集版本的文件:

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

当前显示的应用程序版本为1.0.3842.38865,但较新的应用程序(1.0.3854.42448)最近上传到服务器。

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

问题是浏览器在部署到服务器后不会加载新的Silverlight应用程序。

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

这是一个HTML代码,用于“渲染”silverlight-html-loader(不确定它是否是正确的名称):

<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:

我试图在包含上次XAP文件修改时间的对象的“source”参数中添加一个参数:

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

That caused an error of Silverlight application loading:

这导致Silverlight应用程序加载错误:

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

Silverlight应用程序代码中的未处理错误:2103类别:InitializeError消息:应用程序无效或格式错误:检查清单

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.

附:有必要添加silverlight应用程序在我的localhost上工作(上传和启动)没有任何参数跳舞。只有当我将其上传到网站时 - 它才会被浏览器重新加载。并在xap文件路径中添加其他参数 - 对localhost无效。

4 个解决方案

#1


4  

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

我们目前使用的是以下内容,它获取.xap文件的最后写入时间并将其附加到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 ...

#2


4  

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.

我还在测试,但到目前为止,似乎修改AssmeblyFileVersion会强制浏览器下载最新的xap文件。使用Silverlight 4,我尝试了许多其他提供的解决方案,无法让它们工作或者它们是不受欢迎的(就像没有缓存一样)。现在我正在递增文件版本,它似乎每次都抓住最新的xap。

[assembly: AssemblyFileVersion("1.0.0.1234")]

#3


2  

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

这应该按预期工作,也许它与你如何附加参数有关。尝试删除Ver =部分:

<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:

如果您希望它始终刷新并且永不缓存,您只需将当前DateTime添加到结尾,这也将始终是唯一的。不知道什么时候你想在现实世界的场景中这样做,但它非常适合测试以确保你永远不会运行缓存版本。例如:

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

如果这不起作用,只需将它们全部一起移除,不要添加到末尾,看看它是否加载。我有一种感觉,错误是其他的,因为它并不真正适用于xap文件的位置。

#4


1  

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.

管理浏览器缓存的正确方法是通过服务器发送的适当的http响应标头告诉它您对它的期望。

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

在IIS管理器中,指定ClientBin文件夹的内容立即过期。

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.

请注意,这并不意味着每次请求都会下载Xap,只是浏览器应该检查其缓存副本是否是最新的。

#1


4  

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

我们目前使用的是以下内容,它获取.xap文件的最后写入时间并将其附加到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 ...

#2


4  

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.

我还在测试,但到目前为止,似乎修改AssmeblyFileVersion会强制浏览器下载最新的xap文件。使用Silverlight 4,我尝试了许多其他提供的解决方案,无法让它们工作或者它们是不受欢迎的(就像没有缓存一样)。现在我正在递增文件版本,它似乎每次都抓住最新的xap。

[assembly: AssemblyFileVersion("1.0.0.1234")]

#3


2  

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

这应该按预期工作,也许它与你如何附加参数有关。尝试删除Ver =部分:

<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:

如果您希望它始终刷新并且永不缓存,您只需将当前DateTime添加到结尾,这也将始终是唯一的。不知道什么时候你想在现实世界的场景中这样做,但它非常适合测试以确保你永远不会运行缓存版本。例如:

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

如果这不起作用,只需将它们全部一起移除,不要添加到末尾,看看它是否加载。我有一种感觉,错误是其他的,因为它并不真正适用于xap文件的位置。

#4


1  

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.

管理浏览器缓存的正确方法是通过服务器发送的适当的http响应标头告诉它您对它的期望。

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

在IIS管理器中,指定ClientBin文件夹的内容立即过期。

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.

请注意,这并不意味着每次请求都会下载Xap,只是浏览器应该检查其缓存副本是否是最新的。