executionTimeout在asp.net mvc上不起作用

问题描述:

我尝试在web.config中为asp.net mvc应用程序设置executionTimeout.

I tried set the executionTimeout in web.config for an asp.net mvc application.

<location path="Home/Index">
    <system.web>
      <httpRuntime  executionTimeout="5"/>
    </system.web>
  </location>

任何人在索引操作中使用了 Thread.Sleep

any used the Thread.Sleep in Index action

public ActionResult Index()
{
    Thread.Sleep(30000);            
    return View();
}

此外,我将编译的调试设置为"false" .在该动作之后睡眠约30秒,并且请求超时"将在步骤3中显示.不会抛出异常,并且视图已成功呈现.

also, i set complilation's debug to "false". after the action sleep about 30 seconds and the "request timeout" exception not throws out and the view had been rendered successfully.

有人知道如何使executionTimeout在asp.net mvc中工作吗?

any one know how to make the executionTimeout to work in asp.net mvc?

您需要满足以下条件:

  1. 域名不是localhost(要测试超时,应使用"YourComputerName"而不是"localhost").
  2. 项目在发布模式下编译.
  3. < compilation debug ="false">

然后,请考虑以下问题:

Then also, think about this:

内部,ASP.NET使用Timer来调用请求取消过程.该计时器每15秒触发一次,因此,如果 executionTimeout 设置为3秒,则实际上请求可以在3秒至18秒之间的任何时间超时.

Internally ASP.NET uses a Timer to invoke the request cancelation process. This timer is fired once every 15 seconds, so if the executionTimeout is set to 3 seconds, in reality the request can timeout at any time between 3 seconds and 18 seconds.

触发计时器时,将使用ThreadPool中的线程检查所有请求.通过在执行请求的线程上调用Abort,将超时的线程发送给 ThreadAbortException .

When the timer is fired, a thread from the ThreadPool is used to check all the requests. The ones that have timed out are sent a ThreadAbortException by calling Abort on the thread executing the request.

注意:请记住,只能通过托管代码来观察 ThreadAbortException .因此,如果您的线程正在调用某些非托管函数,则该线程将不会被中止,因此不会强制执行超时,直到执行返回到托管世界为止.这可以是任意长度的延迟,具体取决于那些非托管代码的作用.

Note: Keep in mind that ThreadAbortException can only be observed by managed code. So if you thread is calling some unmanaged functions, the thread will not be aborted, and therefore the timeout will not be enforced, until the execution returns to the managed world. That can be an arbitrary length of delay depending on what those unmanaged code does.

了解更多: 查看更多