ASP.NET executionTimeout未抛出异常

4

我正在尝试在ASP.NET MVC中实现执行超时。

我已经了解到,执行超时只有在compilation debug="false"的情况下每15秒才进行一次评估,详情请参见http://blogs.msdn.com/b/pedram/archive/2007/10/02/how-the-execution-timeout-is-managed-in-asp-net.aspx

然而,我似乎无法触发超时。

我有一个基本的MVC应用程序(来自模板),并进行了以下更改:

web.config:

<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5" executionTimeout="1" />

HomeController:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        Thread.Sleep(20000);
        throw new Exception("Shouldnt get here as its longer than the execution timeout +15s");
    }
}

如果执行超时时间为1秒,而控制器中的睡眠时间为20秒,则总是超过超时时间(最大16秒),但当我运行应用程序时,总是会出现异常。

为了在超时时终止线程,我需要做什么?


我猜Thread.Sleep只是暂停执行指定的时间,它不会超时,因为执行将在那段时间之后重新开始。 - Mairaj Ahmad
@MairajAhmad 我已经改变了代码以使用SpinWait,但它仍然会触发异常,我认为这可能是因为脱水引起的,但只有在异步等待时才会出现。 - undefined
@MairajAhmad 有趣的是,即使我等待了125秒(110 + 15),我仍然遇到了异常。 - undefined
你解决了这个问题吗?我也遇到了同样的问题。 - Brian
@Brian 嗯,我还是不知道如何解决这个问题... - undefined
显示剩余5条评论
1个回答

0

在这种情况下,睡眠功能无法正常工作。

您需要让它执行实际的处理,例如在一个for循环中嵌套另一个for循环来执行某些操作。例如:

var t = 1;
for (int i = 0; i < 99999999; i++)
{
    for (int i2 = 0; i2 < 99999999; i2++)
    {
        for (int i3 = 0; i3 < 99999999; i2++)
        {
            t = i + i2 - i3;
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接