对HTTP处理程序的同时请求不起作用

Rob*_*obV 3 asp.net iis iis-7 httphandler

我的ASP.Net应用程序中有一个通用HTTP处理程序(*.ashx),它执行一些基本但耗时的计算,将进度语句打印到输出,以便让用户知情.执行这些计算涉及读取处理程序在使用时锁定的一些数据文件,因此对Handler的两次调用不会立即开始处理.

为了实现这一点,我在Cache中添加了一个变量,指示计算正在进行中,这可以防止主应用程序在另一个用户已经存在的情况下将用户发送到此Handler.在Handler本身中,它检查Cache变量是否已设置,如果设置了Cache值,则应将用户发送回主应用程序.但是当我通过访问Handler两次测试这个时,一个访问执行正常,第二个坐在那里并且在第一个完成运行时什么都不做.将IsReusable设置为true没有任何区别.

有人知道为什么会这样吗?

代码如下:

public class UpdateStats : IHttpHandler
{
    private HttpContext _context;

    public const String UpdateInProgressCacheKey = "FAHLeagueWebUpdateInProgress";

    public void ProcessRequest(HttpContext context)
    {
        //Use a Cache variable to ensure we don't call multiple updates
        Object inprogress = context.Cache[UpdateInProgressCacheKey];
        if (inprogress != null)
        {
            //Already updating
            context.Response.Redirect("Default.aspx");
        }
        else
        {
            //Set the Cache variable so we know an Update is happening
            context.Cache.Insert(UpdateInProgressCacheKey, true, null, DateTime.Now.AddMinutes(10), Cache.NoSlidingExpiration);
        }

        context.Response.Clear();
        context.Response.ContentType = "text/html";
        this._context = context;

        context.Response.Write("<pre>Please wait while we Update our Statistics, you will be automatically redirected when this finishes...\n\n");

        //Get the Stats
        Statistics stats = new Statistics(context.Server);

        //Subscribe to Update Progress Events
        stats.UpdateProgress += this.HandleUpdateProgress;

        //Update
        String force = context.Request.QueryString["force"];
        stats.UpdateStats((force != null));

        //Remove the Cache variable
        context.Cache.Remove(UpdateInProgressCacheKey);

        context.Response.Write("</pre>");
        context.Response.Write("<meta http-equiv=\"refresh\" content=\"0;URL=Default.aspx\" />");
        context.Response.Write("<p>If you are not automatically redirected please click <a href=\"Default.aspx\">here</a></p>");
    }

    private void HandleUpdateProgress(String message)
    {
        this._context.Response.Write(message + "\n");
        this._context.Response.Flush();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

添加了主应用程序主页面中的代码:

public partial class FAH : System.Web.UI.MasterPage
{
    private Statistics _stats;

    protected void Page_Init(object sender, EventArgs e)
    {
        this._stats = new Statistics(this.Server);
        if (this._stats.StatsUpdateNeeded)
        {
            //If the Cache variable is set we're already updating
            Object inprogress = Cache[UpdateStats.UpdateInProgressCacheKey];
            if (inprogress != null) this.Response.Redirect("UpdateStats.ashx");
        }
    }
    //etc...
}
Run Code Online (Sandbox Code Playgroud)

Rob*_*obV 6

我自己偶然发现了答案,它与Web服务器或应用程序无关,而仅与浏览器行为有关.似乎如果您打开多个选项卡并在浏览器(如Firefox或Chrome)中导航到相同的URL,则浏览器会按顺序发出请求,即在下一个选项之前等待一个完成.打开两个浏览器并发出两个请求会导致预期的行为

IIS,Asp.NET管道和并发