asp.net - 对 HTTP 处理程序的同时请求不起作用

标签 asp.net iis iis-7 httphandler

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

为了实现这一点,我在缓存中添加了一个变量,指示计算正在进行中,这可以防止主应用程序在另一个用户已经存在的情况下将用户发送到该处理程序。在 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;
        }
    }
}

编辑

从主应用程序的母版页添加代码:

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

最佳答案

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

IIS, Asp.NET pipeline and concurrency

关于asp.net - 对 HTTP 处理程序的同时请求不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/985604/

相关文章:

c# - "Requested URL cannot be found"在IIS上发布网站时

iis-7 - IIS7 设置文件位置

asp.net - 对于同一 IIS 服务器 ASP.NET 上的 2 个应用程序, session 丢失太快

c# - 通过多个表单 POST 持久化数据

c# - 通过查询参数处理 .NET Core 3.1 Web API 中的多个端点

c# - 如何通过iframe获取另一个页面的asp隐藏字段值

iis - SSRS报表服务器是否在IIS之上运行?

c# - 搜索过滤 - DropDownLists 和 TextBoxes

http - 批量请求 MIME 类型的 IIS 压缩设置 'multipart/mixed'

c# - 在 IIS 中重新启动应用程序池时立即运行 Application_Start