我如何设置一个控制器操作的请求超时在IIS和IIS Express中的asp.net mvc的5 6的应用 [英] How do I set the request timeout for one controller action in an asp.net 5 mvc 6 application on IIS and IIS Express

查看:345
本文介绍了我如何设置一个控制器操作的请求超时在IIS和IIS Express中的asp.net mvc的5 6的应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要增加一个特定的控制器操作的请求超时在我的应用程序编程控制器。

I need to increase the request timeout for a specific controller action in my application programmatically in the controller.

推荐答案

到目前为止,我已经找到处理一个长时间运行脚本(如果您的应用程序根本无法避免的最佳途径之一)是先创建一个异步处理程序:

The best way I have found so far to handle a long running script (if your application simply can't avoid having one) is to firstly create an async handler:

public async Task<IActionResult> About()
    {
            var cts = new CancellationTokenSource();            
            cts.CancelAfter(180000);
            await Task.Delay(150000, cts.Token);
        return View();
    }

在上面的代码段我添加了一个取消令牌说,我的异步方法180秒内应该取消(即使在这个例子中它永远不会拖延更长的时间超过150秒),我已经添加了超时,以确保没有对我的脚本多久可以运行一个上限。

In the snippet above I have added a Cancellation token to say that my Async Method should cancel after 180 seconds (even though in this example it will never delay for longer than 150 seconds) I've added the timeout to ensure there is a cap on how long my script can run.

如果你运行这个你会得到一个HTTP错误502.3因为有上00:02:00的httpPlatform设置超时。

If you run this you will get a HTTP Error 502.3 since there is a timeout set on the httpPlatform of 00:02:00.

要延长此超时去wwwroot文件夹里面的web.config文件和将requestTimeout =00:05:00属性添加到httpPlatform元素。例如:

To extend this timeout go to the web.config file inside the wwwroot folder and add a requestTimeout="00:05:00" property to the httpPlatform element. For Example:

<httpPlatform requestTimeout="00:05:00" processPath="%DNX_PATH%" arguments="%DNX_ARGS%" stdoutLogEnabled="false" startupTimeLimit="3600"/>



长时间运行脚本现在应该工作。

The long running script should now work.

这不,但是,意味着任何请求都将采取5分钟,超时,所以你必须确保你使用的CancellationToken伎俩限制任何异步请求您在你的申请。在MVC的早期版本中,我们有AsyncTimeOut属性,你可以添加到一个控制器,但现在看来,这已经MVC6的见GitHub的ASPNET / MVC问题#2044

This does, however, mean that any request will take 5 minutes to timeout so you will have to ensure that you use the CancellationToken trick to limit any Async requests you have in your application. In previous versions of MVC we had the AsyncTimeOut attribute that you could add to a controller, but it appears that this has been cut in MVC6 See GitHub aspnet/mvc Issue #2044

这篇关于我如何设置一个控制器操作的请求超时在IIS和IIS Express中的asp.net mvc的5 6的应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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