我的 ASP.Net 应用程序中有一个通用 HTTP 处理程序 (*.ashx),它执行一些基本但耗时的计算,将进度语句打印到输出,以便让用户了解情况。执行这些计算涉及读取一些在使用处理程序时锁定的数据文件,因此对处理程序的两次调用不要立即开始处理,这一点很重要。
为了实现这一点,我在缓存中添加了一个变量,指示计算正在进行中,这可以防止主应用程序在另一个用户已经存在的情况下将用户发送到该处理程序。在处理程序本身中,它检查是否设置了 Cache 变量,如果设置了 Cache 值,则应将用户发送回主应用程序。但是,当我通过访问处理程序两次来测试这一点时,一次访问执行正常,而第二次访问则停留在那里并且不执行任何操作,直到第一次访问运行完成为止。将 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;
}
}
}
Edit
添加了主应用程序母版页中的代码:
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 管道和并发性 https://stackoverflow.com/questions/307936/iis-asp-net-pipeline-and-concurrency
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)