长时间运行的 ASP.NET MVC 核心控制器 HTTPPost 方法超时 [英] Timeouts with long running ASP.NET MVC Core Controller HTTPPost Method
问题描述
我在我的 ASP.NET Core MVC
视图页面中使用了 ajax 调用
I make use of ajax call in my ASP.NET Core MVC
view pages
MyView.cshtml
MyView.cshtml
$.ajax({
processData: false,
contentType: false,
data: new FormData(this),
type: $(this).attr('method'),
url: $(this).attr('action'),
cache: false,
success: function (data) {
$('#mydiv).html(data);
$('#bootstrapModal).modal('show');
控制器发布方法"
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> MyLongRunningMethod(MyViewModel viewModel)
{
await MyProcess.Longprocess1();
await MyProcess.Longprocess2();
await MyProcess.Longprocess3();
await MyProcess.Longprocess4();
return PartialView("_MyPartialPage");
}
这有效,但只有长时间运行的进程有问题.在调试模式下,当该过程需要超过大约 2 分钟时,我收到以下错误
This works but only has an issue for a long running process. I get the following error during debug mode when the process takes longer than around 2 minutes
我知道这与过期超时有关
I understand this is to do with expiration timeout
在以前版本的 ASP.NET MVC 中,您显然可以增加 ASP.NET 控制器操作的超时时间.
in previous versions of ASP.NET MVC you can apparently increase the timeout in your asp.net controller action.
HttpContext.Current.Server.ScriptTimeout = 90000;
然而,这在 ASP.NET Core
我想增加调试和部署特定 asp.net 控制器的超时时间.
I want to increase the timeout for debugging and deployment for a particular asp.net controller.
对于生产,我可以通过将 requestTimeout 添加到现有的 httpPlatform 标记,在 web.config
中全局设置它.例如10分钟
For production I can set it globally in the web.config
by adding requestTimeout to the existing httpPlatform tag.
e.g. for 10 minutes
<httpPlatform requestTimeout="00:10:00" ....
问了一个类似的问题,但 answer 使用 CancellationToken
给出但阅读它,它似乎不能帮助我解决超时问题.
A similar question was asked but the answer giving using an CancellationToken
but reading it, it doesn't seem it can help me with the timeouts.
- 如何像部署时一样在 Visual Studio 2015 调试模式下设置超时?
- 是否有像
ASP.NET 4
中那样的每个控制器超时设置?
- How do I set the timeouts in Visual Studio 2015 debug mode like I can do when I deploy it?
- IS there a per controller timeout setting like there was in
ASP.NET 4
?
推荐答案
但是在 .Net Core 2.0 中,项目中没有 web.config
文件.自动生成.
But in .Net Core 2.0 there is no web.config
file in project. It generate automatically.
我通过将 .UseKestrel(...)
添加到 Program.cs
文件中的 BuildWebHost
函数解决了这个问题,如下所示:
I solved the problem by adding .UseKestrel(...)
to the BuildWebHost
function in Program.cs
file as follows:
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.UseKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); })
.Build();
}
这篇关于长时间运行的 ASP.NET MVC 核心控制器 HTTPPost 方法超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!