.NET Execution Timeout Not Taking Effect in an MVC Web Project

前提是你 提交于 2019-12-21 04:42:30

问题


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

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">

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();
    }

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...


回答1:


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);
    }
}

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

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

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

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


来源:https://stackoverflow.com/questions/11657381/net-execution-timeout-not-taking-effect-in-an-mvc-web-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!