Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Asp.Net服务器端输出缓存在每个请求中都会被命中(HttpHandler)_C#_Asp.net_Caching_Server Side - Fatal编程技术网

C# Asp.Net服务器端输出缓存在每个请求中都会被命中(HttpHandler)

C# Asp.Net服务器端输出缓存在每个请求中都会被命中(HttpHandler),c#,asp.net,caching,server-side,C#,Asp.net,Caching,Server Side,下面我有一段非常直接的代码 有几件事我都试过了,我不明白为什么服务器端输出缓存不能正常工作。下面是最后一次尝试“缓存设置”,以不查看调试输出窗格上的点击 它快把我逼疯了!我该如何防止被击中。。。?!当我打开Developer tools并选中Disable cache时,我希望得到一个缓存的服务器端副本,但在Debug output窗格中看不到命中 我使用的是Windows 8,但即使在另一个Windows版本(/IIS版本)上,我也无法想象最终的代码会有所不同 using System; us

下面我有一段非常直接的代码

有几件事我都试过了,我不明白为什么服务器端输出缓存不能正常工作。下面是最后一次尝试“缓存设置”,以查看调试输出窗格上的点击

它快把我逼疯了!我该如何防止被击中。。。?!当我打开Developer tools并选中Disable cache时,我希望得到一个缓存的服务器端副本,但在Debug output窗格中看不到命中

我使用的是Windows 8,但即使在另一个Windows版本(/IIS版本)上,我也无法想象最终的代码会有所不同

using System;
using System.Diagnostics;
using System.Net.Http;
using System.Web;

namespace WebApplication1
{
    /// <summary>
    /// Summary description for MyHandler
    /// </summary>
    public class MyHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            Debug.WriteLine("HIT"+DateTime.Now.ToLongTimeString());

            context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5));
            context.Response.Cache.SetCacheability(HttpCacheability.Server);
            context.Response.Cache.SetValidUntilExpires(true);
            context.Response.Cache.SetOmitVaryStar(true);
            context.Response.Cache.VaryByParams["none"] = true;
            context.Response.Cache.SetMaxAge(new TimeSpan(0, 5, 0));

            // Just here for testing purposes
            const string url = "http://otherserver/image.png";
            using (var client = new HttpClient())
            {
                var task = client.GetStreamAsync(url);
                task.Wait();
                task.Result.CopyTo(context.Response.OutputStream);
                context.ApplicationInstance.CompleteRequest();
            }
        }

        public bool IsReusable
        {
            get { return true; }
        }
    }
}
使用系统;
使用系统诊断;
使用System.Net.Http;
使用System.Web;
命名空间WebApplication1
{
/// 
///MyHandler的摘要说明
/// 
公共类MyHandler:IHttpHandler
{
公共void ProcessRequest(HttpContext上下文)
{
Debug.WriteLine(“HIT”+DateTime.Now.ToLongTimeString());
context.Response.Cache.SetExpires(DateTime.Now.AddMinutes(5));
context.Response.Cache.SetCacheability(HttpCacheability.Server);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.SetOmitVaryStar(true);
context.Response.Cache.VaryByParams[“none”]=true;
context.Response.Cache.SetMaxAge(新的时间跨度(0,5,0));
//只是为了测试的目的
常量字符串url=”http://otherserver/image.png";
使用(var client=new HttpClient())
{
var task=client.GetStreamAsync(url);
task.Wait();
task.Result.CopyTo(context.Response.OutputStream);
context.ApplicationInstance.CompleteRequest();
}
}
公共布尔可重用
{
获取{return true;}
}
}
}

事实证明,
ApplicationInstance.CompleteRequest()
给我的处境带来了痛苦。据它说:

使ASP.NET绕过HTTP管道执行链中的所有事件和筛选,直接执行EndRequest事件

这是

处理程序正在执行中,如您所见,调用“代码>应用程序实例.SealErrEdSee())/代码>它跳过所有的东西,直接发送请求(或代码>完整的RealStices)(< /代码>内部)。 发生这种情况时,它也会跳过“更新缓存”事件。这是请求缓存更新的地方;将在其中添加服务器端输出缓存项

因此,当您认为自己在
HttpHandler
中完成时,请注意
ApplicationInstance.CompleteRequest()
的功能

另一个有趣的阅读:

快乐的缓存