【问题标题】:asp.net core 2 Web API timeout issueasp.net core 2 Web API 超时问题
【发布时间】:2019-04-30 19:14:34
【问题描述】:

我有一个 .net 核心 web api,其中一个端点运行一个存储过程,需要 3-4 分钟才能完成。 API 部署到 IIS。

当我创建一个 httpGet 时,我得到 502 Bad Gateway 错误。查看IIS Log,错误实际上是超时。这来自 IIS 日志:

2018-11-28 17:24:48 10.71.12.59 GET /api/meetingreport fromDate=11/01/2018&toDate=11/30/2018&tradingGroup=All&symbol=&reviewed= 16000 - 10.6.50.61 Mozilla/5.0+(Windows+NT+6.1;+Win64;+x64)+AppleWebKit/537.36+(KHTML,+like+Gecko)+Chrome/64.0.3282.140+Safari/537.36 - 502 3 12002 120029

错误代码 12202 用于 ERR_WINHTTP_TIMEOUT。 超时发生在 2 分钟后。请求时间少于 2 分钟可以正常工作。我通过添加超过 2 分钟和不到 2 分钟的 thread.sleep 来检查它。

[HttpGet("")]
        public async Task<IActionResult> Get([FromQuery(Name = "fromDate")] DateTime fromDate, 
                    [FromQuery(Name = "toDate")] DateTime toDate, [FromQuery(Name ="tradingGroup")]string tradingGroup, [FromQuery(Name = "symbol")]string symbol, [FromQuery(Name = "reviewed")]string reviewed)
        {
            try
            {
                if (fromDate == DateTime.MinValue || toDate == DateTime.MinValue) return BadRequest("fromDate and toDate is a required Field");
                tradingGroup = tradingGroup == "All" ? tradingGroup.Replace("All", "") : tradingGroup;
                tradingGroup = tradingGroup ?? string.Empty;
                symbol = symbol ?? string.Empty;
                var result = await _meetingReportRepository.GetMeetingReport(fromDate, toDate, tradingGroup, symbol, reviewed);
                Thread.Sleep(132000); // 2.2 minutes
                    return Ok(result);
            }
            catch (Exception ex)
            {
            }
            return BadRequest($"Couldn't Genererate Report");

        }

这是来自 POSTMAN 的错误。

如何将超时时间增加到 10 分钟?任何指针?没有 Web.config 文件。

this 发布之后,我更新了我的 program.cs 以添加 10 分钟的 KeepAliveTimeout。但这没有帮助。我的请求在 2 分钟后仍然超时。

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

编辑 1: 部署到 IIS 时,会创建一个 web.config 文件,其内容如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\project.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
    </system.webServer>
  </location>
</configuration>

我将在此处添加超时以查看它是否有效。

编辑 2: 添加 requestTimeout 就可以了。 IIS 非常忠于 web.config :)。我的问题已经解决了。

<aspNetCore requestTimeout="00:20:00" processPath="dotnet" arguments=".\project.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />

【问题讨论】:

标签: c# asp.net-web-api .net-core asp.net-core-webapi


【解决方案1】:

将 requestTimeout 添加到 web.confg 解决了我的超时问题。

<aspNetCore requestTimeout="00:20:00" processPath="dotnet" arguments=".\project.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />

更好的方法是启动请求,然后按照@steve-land 的建议轮询结果

【讨论】:

    【解决方案2】:

    我意识到这并不是专门回答您的问题,但我建议这里的问题更多是缓慢的请求 - 不是任何相关的 IIS/Postman/.Net 管道超时。

    您是否考虑过更改工作流程以发出单个请求来启动流程,然后轮询结果?

    例如

    1. 发出 POST 请求以在后台线程/任务管理处理器上启动进程,并立即接收某种进程 ID 来标识您的新进程。

    2. 使用 processId 作为参数定期轮询另一个 GET 端点,继续进行直到进程完成后您最终收到结果。

    【讨论】:

    • 是的。这正是我想做的。顺便说一句,我可以通过添加 web.config 来增加超时,然后添加 requestTimeout="00:20:00"
    【解决方案3】:

    可能是 Postman 超时了。如果是这种情况,您可以调整 Postman 设置。

    https://www.getpostman.com/docs/v6/postman/launching_postman/settings

    文件 -> 设置 -> 常规:请求超时

    可能已经默认为无穷大了,不知道是不是我手动改的。

    只是一个想法。

    吉娜

    【讨论】:

    • 谢谢。但不是真的。对于 Infinity,我的请求超时设置为 0。此外,当我调用 api 端点时,我的 Angular UI 也会超时。这 2 分钟设置在某个地方,我无法覆盖。叹息。
    猜你喜欢
    • 1970-01-01
    • 2020-07-11
    • 1970-01-01
    • 2018-08-25
    • 2019-08-06
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2021-07-02
    相关资源
    最近更新 更多