【问题标题】:.NET Execution Timeout Not Taking Effect in an MVC Web Project.NET 执行超时未在 MVC Web 项目中生效
【发布时间】:2012-07-25 19:42:12
【问题描述】:

我们正在尝试设置超时,但请求并没有超时。我们尝试了几种设置方式:

通过将它放在 web.config 中(在应用程序的 web.config 和视图文件夹中)

<httpRuntime executionTimeout="5" />

我们确保我们没有处于调试模式

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

我们甚至尝试在代码中设置脚本超时(即使它应该是同一件事)并添加了一个 Thread.Sleep 3 分钟(以确保我们甚至超过默认超时)而且这个动作仍然有效不超时:

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

它发生在多台机器上,我什至去创建一个全新的解决方案,只对模板进行上述更改,以及一个全新的 IIS 网站应用程序池......仍然无法超时。

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

【问题讨论】:

  • 奇怪的是,如果我们将 Thread.Sleep 放入 Global.asax.cs --> Application_BeginRequest 它会超时,但不会在 Thread.Sleep 处于操作中时。
  • 看起来这个帖子解决了你的问题:forums.asp.net/p/1715081/4723882.aspx/…

标签: c# asp.net asp.net-mvc-3 iis-7


【解决方案1】:

添加到最终解决方案:上面的链接有一种方法可以强制 MVC 遵守当前 HttpContext 的超时

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

因为我们希望它在每个请求上运行,我们为它创建了一个全局操作过滤器

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 中注册它:

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

请记住,此解决方案仍需要 web.config 中的上述 2 行

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

【讨论】:

    猜你喜欢
    • 2019-04-13
    • 1970-01-01
    • 2013-01-21
    • 2020-09-18
    • 2015-06-02
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 1970-01-01
    相关资源
    最近更新 更多