.NET执行超时未生效在MVC Web项目 [英] .NET Execution Timeout Not Taking Effect in an MVC Web Project

查看:534
本文介绍了.NET执行超时未生效在MVC Web项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试设置一个超时,并要求就是不超时。我们试图设置此几种方式:

We're trying to set a timeout, and requests just aren't timing out. We tried setting this several ways:

通过把这个在web.config中(在应用程序的web.config,和一个在views文件夹)

By putting this in the web.config (in both the application's web.config, and the one in the views folder)

<httpRuntime executionTimeout="5" />

我们确信,我们不是在调试模式

We made sure that we were not in debug mode

<compilation debug="false" targetFramework="4.0">

我们甚至试图在code设置脚本超时(即使它应该是同样的事情),并增加了一个Thread.sleep代码为3分钟(确保我们远远高于甚至默认的超时时间),这行动仍然没有超时:

We even tried setting a script timeout in code (even though it's supposed to be the same thing) and added a Thread.Sleep for 3 minutes (to make sure we were well above even the default timeout) And this action still does not time out:

    public ActionResult Index()
    {
        Server.ScriptTimeout = 5;
        Thread.Sleep(60 * 1000 * 3);
        return View();
    }

这是发生在多台机器,我甚至去创造,只有从模板上述变化的全新的解决方案,以及全新的IIS网站应用程序池...还是,不能让一个暂停。

It's happening on multiple machines, and I even went to creating a brand new solution with only the above changes from the template, and a brand new IIS website application pool... still, can't get a timeout.

有我们缺少一些简单的配置设置?这似乎是它应该是很容易做到......

Is there some simple configuration setting we're missing? This seems like it should be easy to do...

推荐答案

要添加到最终的解决方案:上面的链接有办法迫使MVC尊重超时为当前HttpContext

To add to the eventual solution: The link above has a way to force MVC to respect the timeout for the current HttpContext

System.Web.HttpContext.Current.GetType().GetField("_timeoutState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(System.Web.HttpContext.Current, 1);

由于我们希望它在每次请求运行,我们建立了一个全球行动过滤器为它

Because we wanted it to run on EVERY request, we created a global action filter for it

public class Timeoutter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        System.Web.HttpContext.Current.GetType().GetField("_timeoutState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(System.Web.HttpContext.Current, 1);
        base.OnActionExecuting(filterContext);
    }
}

和再注册它,在应用程序的Global.asax中:

And then to register it, in the app's global.asax:

    public static void RegisterGlobalFilters(GlobalFilterCollection filters)
    {
        filters.Add(new Timeoutter());
    }

请记住这个解决方案仍然需要同上面的2 web.config中的行

Keep in mind this solution still requires the same 2 above lines in web.config

<httpRuntime executionTimeout="5" />
<compilation debug="false" targetFramework="4.0">

这篇关于.NET执行超时未生效在MVC Web项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆