繁体   English   中英

长时间运行的超时 ASP.NET MVC Core Controller HTTPPost 方法

[英]Timeouts with long running ASP.NET MVC Core Controller HTTPPost Method

我在我的ASP.NET Core MVC视图页面中使用 ajax 调用

我的视图.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');

Controller 后置方法"

    [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 分钟左右时,我在调试模式下收到以下错误

在此处输入图像描述

我知道这与过期超时有关

在以前版本的 ASP.NET MVC 中,您显然可以增加 asp.net controller 操作中的超时。

HttpContext.Current.Server.ScriptTimeout = 90000;

然而,这在ASP.NET Core中不存在

我想增加特定 asp.net controller 的调试和部署超时。

对于生产,我可以通过将 requestTimeout 添加到现有的 httpPlatform 标记,在web.config中全局设置它。 例如 10 分钟

<httpPlatform requestTimeout="00:10:00" ....

有人问了一个类似的问题,但是使用CancellationToken给出的答案但阅读它,似乎它不能帮助我解决超时问题。

  1. 如何在 Visual Studio 2015 调试模式下设置超时,就像我在部署它时可以做的那样?
  2. 是否有像ASP.NET 4中那样的每个 controller 超时设置?

aspNetCore标记上设置RequestTimeout="00:20:00"并部署站点将导致它不会超时。

问题现在仅在从 Visual Studio 调试模式而不是在发布模式下运行时发生。

注意:在 ASP.NET RTM 中, web.confighttpPlatform标记已替换为aspNetCore

ASP.NET Core 示例
<?xml version="1.0" encoding="utf-8"?> <configuration> <system.webServer> <handlers> <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified"/> </handlers> <aspNetCore requestTimeout="00:20:00" processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\\logs\\stdout" forwardWindowsAuthToken="false"/> </system.webServer> </configuration>

但是在 .Net Core 2.0 中,项目中没有web.config文件。 它自动生成。

我通过将.UseKestrel(...)添加到Program.cs文件中的BuildWebHost函数解决了这个问题,如下所示:

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); })
        .Build();
}

就我而言,即使在更新requestTimeout参数后,我们仍然在 2 分钟时遇到 502 超时错误。 事实证明,红隼有一个 2 分钟的保持活动超时,需要被覆盖:

var host = new WebHostBuilder()
               .UseKestrel(o =>
                    {
                        o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30);
                    }
                )

自 .Net Core 3.1 以来,已接受的答案已更改。 在 NET Core 3.1 中,使用以下实现来增加 KeepAliveTimeout:

程序.cs

public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.ConfigureKestrel(options =>
                {
                    options.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
                });
            });

Web.Config(如果不存在,您需要将配置文件添加到您的项目中。)

根据需要在 web.config 上增加 requestTimeout:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified"/>
    </handlers>
    <aspNetCore requestTimeout="00:15:00"  processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
  </system.webServer>
</configuration>

对我来说这有效:

public static void Main(string[] args)
{
    CreateWebHostBuilder(args).Build().Run();
}

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .ConfigureKestrel(o => { o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(10); });

适用于 .net 内核 6.0

var builder = WebApplication.CreateBuilder(args);

builder.WebHost.ConfigureKestrel(t =>
{
   t.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(15);
});

在 Visual Studio 中,解决方案浏览器中不会出现文件 web.config。 我修复了在解决方案中添加文件并更改了“复制到输出目录”属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM