以编程方式使 Silverlight XAP 文件从浏览器缓存中过期

发布于 2024-08-21 21:15:32 字数 93 浏览 11 评论 0原文

如何防止 Web 浏览器缓存 Silverlight XAP 文件?

我想这样做的原因是在开发过程中我不想手动清除浏览器缓存,我正在寻找服务器端的编程方法。

How to do I prevent a Silverlight XAP file being cached by the web browser?

The reason I want to do this is during development I don't want to manually clear the browser cache, I'm looking for a programmatic approach server side.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

诗笺 2024-08-28 21:15:32

使用 IIS 管理添加自定义标头 Cache-Control ,其值为 no-cache。这将导致浏览器在使用之前检查 XAP 的任何缓存版本是否是最新的。

Using IIS management add a custom header Cache-Control with the value no-cache. That'll cause the browser to check that any cached version of the XAP is the latest before using it.

Hello爱情风 2024-08-28 21:15:32

将查询参数添加到 HTML 页面上元素中 XAP 的 URL:

  • clientBin/MyApp.xap?rev=1
  • clientBin/MyApp.xap?rev=2

它将被忽略并破坏缓存。
在IE8中,有一些缓存管理工具:
打开开发人员工具:

  • 尝试缓存...始终从服务器刷新
  • 尝试缓存...清除该域的浏览器缓存...

Add a query parameter to the URL for the XAP in the element on the HTML Page:

  • clientBin/MyApp.xap?rev=1
  • clientBin/MyApp.xap?rev=2

It will be ignored and break the cache.
In IE8, there are some cache management tools:
Open the Developer tools:

  • Try Cache...Always Refresh from Server
  • Try Cache...Clear Browser Cache for this domain...
心欲静而疯不止 2024-08-28 21:15:32

此处提出的解决方案有些相似迈克尔的,但它是自动的,并保证客户总是会得到新版本。根据您的具体情况,这可能效率较低。

由于 Lars 在他的 评论中表示他不参与Stack Overflow,我在这里复制回复。

<object id="Xaml1" data="data:application/x-silverlight-2,
    "type="application/x-silverlight-2" width="100%" height="100%">

  <%––<param name="source" value="ClientBin/SilverlightApp.xap"/>––%>

  <%     
    string orgSourceValue = @"ClientBin/SilverlightApp.xap";     
    string param;

    if (System.Diagnostics.Debugger.IsAttached)     
    {
        param = "<param name=\"source\" value=\"" + orgSourceValue + "\" />";
    }
    else     
    {     
      string xappath = HttpContext.Current.Server.MapPath(@"") + @"\" + orgSourceValue;

      DateTime xapCreationDate = System.IO.File.GetLastWriteTime(xappath);      

      param = "<param name=\"source\" value=\"" + orgSourceValue + "?ignore=" +
                xapCreationDate.ToString() + "\" />";     
    }

    Response.Write(param);     
  %>

  ....

</object>

The solution presented here is somewhat similar to Michael's but is automatic and guarantees the client will always get a new version. This may be inefficient depending on your situation.

Since Lars says in his comments that he is not on Stack Overflow, I'm copying the response here.

<object id="Xaml1" data="data:application/x-silverlight-2,
    "type="application/x-silverlight-2" width="100%" height="100%">

  <%––<param name="source" value="ClientBin/SilverlightApp.xap"/>––%>

  <%     
    string orgSourceValue = @"ClientBin/SilverlightApp.xap";     
    string param;

    if (System.Diagnostics.Debugger.IsAttached)     
    {
        param = "<param name=\"source\" value=\"" + orgSourceValue + "\" />";
    }
    else     
    {     
      string xappath = HttpContext.Current.Server.MapPath(@"") + @"\" + orgSourceValue;

      DateTime xapCreationDate = System.IO.File.GetLastWriteTime(xappath);      

      param = "<param name=\"source\" value=\"" + orgSourceValue + "?ignore=" +
                xapCreationDate.ToString() + "\" />";     
    }

    Response.Write(param);     
  %>

  ....

</object>
玩物 2024-08-28 21:15:32

创建一个自定义 http 处理程序来处理 *.xap 文件,然后在处理程序内设置缓存选项。

像这样的东西...

using System;
using System.IO;
using System.Web;

public class FileCacheHandler : IHttpHandler
{
    public virtual void ProcessRequest(HttpContext context)
    {
        if (File.Exists(context.Request.PhysicalPath))
        {
            DateTime lastWriteTime = File.GetLastWriteTime(filePath);
            DateTime? modifiedSinceHeader = GetModifiedSinceHeader(context.Request);

            if (modifiedSinceHeader == null || lastWriteTime > modifiedSinceHeader)
            {
                context.Response.AddFileDependency(filePath);
                context.Response.Cache.SetLastModifiedFromFileDependencies();
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.TransmitFile(filePath);
                context.Response.StatusCode = 200;
                context.Response.ContentType = "application/x-silverlight-app";
                context.Response.OutputStream.Flush();
            }
            else
            {
                context.Response.StatusCode = 304;
            }
        }
    }

    public DateTime? GetModifiedSinceHeader(HttpRequest request)
    {
        string modifiedSinceHeader = request.Headers["If-Modified-Since"];
        DateTime modifiedSince;
        if (string.IsNullOrEmpty(modifiedSinceHeader)
          || modifiedSinceHeader.Length == 0
          || !DateTime.TryParse(modifiedSinceHeader, out modifiedSince))
            return null;

        return modifiedSince;
    }
}

Create a custom http handler for handling *.xap files and then set your caching options inside the handler.

Something like this...

using System;
using System.IO;
using System.Web;

public class FileCacheHandler : IHttpHandler
{
    public virtual void ProcessRequest(HttpContext context)
    {
        if (File.Exists(context.Request.PhysicalPath))
        {
            DateTime lastWriteTime = File.GetLastWriteTime(filePath);
            DateTime? modifiedSinceHeader = GetModifiedSinceHeader(context.Request);

            if (modifiedSinceHeader == null || lastWriteTime > modifiedSinceHeader)
            {
                context.Response.AddFileDependency(filePath);
                context.Response.Cache.SetLastModifiedFromFileDependencies();
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.TransmitFile(filePath);
                context.Response.StatusCode = 200;
                context.Response.ContentType = "application/x-silverlight-app";
                context.Response.OutputStream.Flush();
            }
            else
            {
                context.Response.StatusCode = 304;
            }
        }
    }

    public DateTime? GetModifiedSinceHeader(HttpRequest request)
    {
        string modifiedSinceHeader = request.Headers["If-Modified-Since"];
        DateTime modifiedSince;
        if (string.IsNullOrEmpty(modifiedSinceHeader)
          || modifiedSinceHeader.Length == 0
          || !DateTime.TryParse(modifiedSinceHeader, out modifiedSince))
            return null;

        return modifiedSince;
    }
}
深海少女心 2024-08-28 21:15:32

我在 xap 文件的路径中添加了一个查询参数,以便我可以通过版本控制来管理它。

Default.aspx 代码:

<param
   name="source"
   value="ClientBin/MySilverLightApp.xap?xapid<%=XapID %>" />

Default.aspx.cs 代码:

protected string XapID
{
    get
    {
        Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

        if (System.Diagnostics.Debugger.IsAttached)
            Response.Write(string.Format("Build: {0}.{1}.{2}.{3}", v.Major.ToString(), v.Minor.ToString(), v.Build.ToString(), v.Revision.ToString()));
        return string.Format("{0}.{1}.{2}.{3}", v.Major.ToString(), v.Minor.ToString(), v.Build.ToString(), v.Revision.ToString()
    }
}

I added a query parm to the path of the xap file, so that I can manage it through Versioning.

Default.aspx code:

<param
   name="source"
   value="ClientBin/MySilverLightApp.xap?xapid<%=XapID %>" />

Default.aspx.cs code:

protected string XapID
{
    get
    {
        Version v = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;

        if (System.Diagnostics.Debugger.IsAttached)
            Response.Write(string.Format("Build: {0}.{1}.{2}.{3}", v.Major.ToString(), v.Minor.ToString(), v.Build.ToString(), v.Revision.ToString()));
        return string.Format("{0}.{1}.{2}.{3}", v.Major.ToString(), v.Minor.ToString(), v.Build.ToString(), v.Revision.ToString()
    }
}
天冷不及心凉 2024-08-28 21:15:32

好吧,以上所有示例都依赖于浏览器不缓存包含新技巧 xap 名称的 HTML...,因此您只需将问题转移到其他问题上即可。
而且它们也极其复杂......

但是对于调试情况,至少,很容易编写 。和 <参数> javascript 中的标签,以便每次使用 html 页面时名称都会更改,无论浏览器是否缓存!

<script type="text/javascript">
   document.write('<object blah blah >');
   document.write('<param name="Source" value="myapp.xap?'
               + new Date().getTime()+'">');
   document.write('</object>'); 
</script>

这避免了您在控制服务器设置时可能遇到的任何麻烦,并且无论使用何种服务器技术,都可以正常工作。

注意:您必须使用相同的方法编写整个对象组,因为将脚本标记放入对象标记中意味着“仅在浏览器不支持该对象时才执行此操作。

Well all the above examples depend on the browser NOT caching the HTML that contains the new trick xap name.... so you just move the problem on to something else.
And they are also fiendishly complicated....

However for the debugging case, at least, it's easy to write the <object> and <param> tags in javascript so that the name changes every time the html page is used, whether it's cached by the browser or not!

<script type="text/javascript">
   document.write('<object blah blah >');
   document.write('<param name="Source" value="myapp.xap?'
               + new Date().getTime()+'">');
   document.write('</object>'); 
</script>

This sidesteps any hassle you may have controlling server settings and works just as well regardless of the server technology in use.

Note: you have to write the whole object group with the same method because putting a script tag inside the object tag means "only do this if the browser doesnt support the object.

〗斷ホ乔殘χμё〖 2024-08-28 21:15:32

遇到 .XAP 缓存的情况并不少见,这意味着每次部署新版本的 Silverlight 应用程序时,浏览器都不会下载更新的 .XAP 文件。

一种解决方案是更改 IIS 属性。您可以按照以下步骤为您的 .XAP 文件打开“启用内容过期 HTTP 标头”选项:

  1. 打开 IIS 管理器
  2. 转到“默认网站”并找到您的 Silverlight 项目的网站。
  3. 在 ClientBin 下找到 .XAP 文件。
  4. 转到.XAP文件的属性页,在HTTP标头选项卡上,打开“启用内容过期”,单击“立即过期”单选按钮。
  5. 保存更改。

这样,当您刷新页面时,就会下载最新的 .XAP(仅当有最新的 .XAP 文件时),而无需关闭浏览器。

希望这有帮助!

It’s not very uncommon to run into .XAP caching, which means that every time you deploy a new version of the Silverlight application, the browser does not download the updated .XAP file.

One solution could be to change the IIS properties. You can turn the “Enable Content Expiration HTTP header” option on for your .XAP file by following these step:

  1. Open IIS Manager
  2. Go to “Default Web Site” and find web site for your Silverlight project.
  3. Find the .XAP file under ClientBin.
  4. Go to the properties page of the .XAP file, on HTTP Headers Tab, Turn on “Enable Content Expiration”, click the “Expire Immediately” radio button.
  5. Save the changes.

This way the latest .XAP (only if there is a latest .XAP file) will get downloaded when you refresh your page without having to close the browser.

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文