代码之家  ›  专栏  ›  技术社区  ›  ProgSky

ASP.NET Core 2 Web API超时问题

  •  4
  • ProgSky  · 技术社区  · 6 年前

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

    当我做一个httpget时,我得到502坏网关错误。查看IIS日志,错误实际上是超时。来自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分钟。

    [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");
    
            }
    

    这是邮递员的错误。

    enter image description here

    如何将超时时间增加到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" />
    
    3 回复  |  直到 6 年前
        1
  •  2
  •   Steve Land    6 年前

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

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

    例如。

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

    2. 使用processID作为参数定期轮询另一个get端点,直到流程完成后最终收到结果为止。

        2
  •  1
  •   ProgSky    6 年前

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

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

    更好的方法是启动请求,然后按照@steve land的建议对结果进行投票。

        3
  •  0
  •   Gina Marano    6 年前

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

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

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

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

    只是一个想法。

    吉娜