asp.net - Page.AsyncTimeout - 无限超时?

标签 asp.net asynchronous c#-5.0 asp.net-4.5

我看到了an example forever iframe 实现( cometd 模拟),所以我决定对其进行测试,但添加了异步方法,这样就不会出现阻塞。

很简单:

我有一个带有隐藏 iframe 的页面 (index.html) 其 SRC 为 AdminPush.aspx :

/*1*/           protected void Page_Load(object sender, EventArgs e)
/*2*/           {
/*3*/               UpdateMessage();
/*4*/           }
/*5*/   
/*6*/   
/*7*/           protected void UpdateMessage()
/*8*/           {
/*9*/               HttpContext.Current.Response.ContentType = "text/html";
/*10*/               Response.Write("<script  >parent.UpdateMessage(DateTime.Now.Second)</script>");
/*11*/               Response.Flush();
/*12*/   
/*13*/               //async part goes here  !!
/*14*/               this.RegisterAsyncTask(new PageAsyncTask(async cancellationToken =>
/*15*/                                       {
/*16*/                                            await Task.Delay(2000, cancellationToken);
/*17*/                                            UpdateMessage();
/*18*/                                       }));
/*19*/           }

AdminPush.aspx 页面上我添加了:

Async="true"

在 html 页面 (index.html) 我添加了:

function UpdateMessage(Message)
        {
          console.log(Message);
        }


    function setupAjax() //body Onload - calls it.
    {
        var iframe = document.createElement("iframe");
        iframe.src = "adminpush.aspx"; 
        iframe.style.display = "none";
        document.body.appendChild(iframe);
    }

所以基本上 iframe 被注入(inject)了脚本命令,它更新了 iframe 的父级 index.html

它正在工作。

但是当我测试它时 - 它在 45 秒后停止更新。

我认为它与 web.config 中的 requestTimeout 属性有关 - 但事实并非如此。

这与 AdminPush.aspx 页面中缺少的 AsyncTimeOut 属性有关。

问题 #1:

根据 msdn异步超时:

Gets or sets a value indicating the time-out interval used when processing asynchronous tasks.

但它也说:

A TimeSpan that contains the allowed time interval for completion of the asynchronous task. The default time interval is 45 seconds.

请注意我每次“延迟”2秒

起初我将超时设置为 1 分钟,但后来也失败了。我认为超时应该与 each operation 相关,而不是 sum(all async operations)

为什么会这样?它应该是异步任务超时! (单个)但它表现为 sum(tasks)

这里的措辞具有误导性。任何澄清?

问题 #2:

我需要将它设置为max 值。 什么那个值是什么?但是,我仍然需要它支持浏览器很长时间。所以恐怕这个值也无济于事。

有什么方法可以重置这个值(在 n 个周期后)?

我知道还有其他解决方案/库(如 signalR)正在做这项工作,但它并不妨碍学习其他东西是如何完成的。

最佳答案

异步页面的想法是释放 IIS,以便可以为更多用户提供服务,如果您创建一个“永不”完成的页面,您将耗尽所有资源。

话虽如此...如果您还想这样做...

我们“知道”(文档),异步页面通过将页面的执行分成 2 部分来工作......后台任务之前的所有内容和任务之后的所有内容,这样 IIS 可以在后台任务完成时处理更多请求他们的工作。 (还有更多,但现在就足够了)

所以...他们“必须”正在创建某种任务管理器(如根/主任务)来执行所有按顺序注册的任务,这样 IIS 开始处理页面,启动任务管理器,释放 IIS,任务管理器继续处理任务,当它完成时,它将控制返回给 IIS。

这可以解释为什么 AsyncTimeout 控制所有已注册的任务而不是一个一个地控制(超时实际上应用于任务管理器)。

我用 6000 秒的超时测试了你的代码的一个变体并且它有效:

C#:

protected void Page_Load(object sender, EventArgs e)
{
    Page.RegisterAsyncTask(new PageAsyncTask(ProcessTask));
}

protected async Task ProcessTask()
{
    await Task.Delay(1000);
    Response.Write(DateTime.Now.ToLongTimeString() + "<br/>");
    Response.Flush();
    Page.RegisterAsyncTask(new PageAsyncTask(ProcessTask));
}

aspx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Sample03.Default" Async="true" AsyncTimeout="6000" %>

希望对您有所帮助。

关于asp.net - Page.AsyncTimeout - 无限超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23404212/

相关文章:

asp.net - 匹配最多五个单词的正则表达式

asp.net - 如何在 web.config 中加密连接

python - Python 中的协程与 Lua 中的协程相比如何?

javascript - nodejs异步并行内部异步每个

javascript - async.auto 结果对象应该有一个值,但有一个包含两个值的数组

c# - XElement 在 C# 中使用 async/await 保存文件

c# - 如何编写 C# 5 异步?

asp.net - 使用 Create-React-App,是否可以像使用 grunt/gulp 一样在构建过程中将代码注入(inject)/替换/附加到 index.html 文件中?

multithreading - 我们是否应该使用Task.Delay替换Thread.Sleep

c# - ASP.NET 从 WebService 获取项目并插入到 dropDownList