DrS*_*lis 6 c# asp.net iis-7 asp.net-mvc-3
我们正在尝试设置超时,并且请求不会超时.我们尝试过以下几种方式:
将它放在web.config中(在应用程序的web.config中,以及views文件夹中的那个)
<httpRuntime executionTimeout="5" />
Run Code Online (Sandbox Code Playgroud)
我们确保我们没有处于调试模式
<compilation debug="false" targetFramework="4.0">
Run Code Online (Sandbox Code Playgroud)
我们甚至尝试在代码中设置脚本超时(即使它应该是相同的东西)并添加一个Thread.Sleep 3分钟(以确保我们远远高于默认超时)并且此操作仍然没有超时:
public ActionResult Index()
{
Server.ScriptTimeout = 5;
Thread.Sleep(60 * 1000 * 3);
return View();
}
Run Code Online (Sandbox Code Playgroud)
它发生在多台机器上,我甚至创建了一个全新的解决方案,只有模板中的上述更改,以及一个全新的IIS网站应用程序池...仍然无法超时.
我们缺少一些简单的配置设置吗?这看起来应该很容易......
添加到最终解决方案:上面的链接有一种方法可以强制MVC尊重当前HttpContext的超时
System.Web.HttpContext.Current.GetType().GetField("_timeoutState", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic).SetValue(System.Web.HttpContext.Current, 1);
Run Code Online (Sandbox Code Playgroud)
因为我们希望它在每个请求上运行,所以我们为它创建了一个全局动作过滤器
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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后在应用程序的global.asax中注册它:
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new Timeoutter());
}
Run Code Online (Sandbox Code Playgroud)
请记住,此解决方案仍需要web.config中相同的2行
<httpRuntime executionTimeout="5" />
<compilation debug="false" targetFramework="4.0">
Run Code Online (Sandbox Code Playgroud)