Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net core 2 Web API timeout issue

I have a .net core web api and one of the end point runs a stored procedure that takes 3-4 minutes to complete. API is deployed to IIS.

When I make a httpGet , I get 502 Bad Gateway error. Looking at the IIS Log, the error is actually timeout. This from IIS Log:

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

Error code 12202 is for ERR_WINHTTP_TIMEOUT. Timeout happens after 2 mins. Request taking under than 2 mins works fine. I checked it by adding a thread.sleep of over and under 2 mins.

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

        }

This is the error from POSTMAN.

enter image description here

How can I increase the timeout to 10 minutes ? Any pointers? There is no Web.config file.

Following this post I updated my program.cs to add KeepAliveTimeout of 10 minutes. But this didn't help . My request is still timing out after 2 mins.

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

EDIT 1: When deployed to IIS, a web.config file is created and it has content as below:

<?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>

I am going to add timeout here to see if it works.

EDIT 2: adding requestTimeout did the trick. IIS is very loyal to web.config :). My issue is resolved.

<aspNetCore requestTimeout="00:20:00" processPath="dotnet" arguments=".\project.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" />
like image 239
ProgSky Avatar asked Nov 28 '18 19:11

ProgSky


2 Answers

adding requestTimeout to web.confg solved my timeout.

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

Better approach is to Kick off request and then poll the result as suggested by @steve-land

like image 191
ProgSky Avatar answered Nov 11 '22 19:11

ProgSky


I realise that this isnt specifically answering your question, but I'd suggest that the problem here is more the slow request - not any related IIS/Postman/.Net pipeline timeout(s).

Have you considered changing your workflow to make a single request to kick off the process and then polling for the result?

E.g.

  1. Make a POST request to start the process on a background thread / task management processor, and immediately receive some kind of process Id identifying your new process.

  2. Periodically poll another GET endpoint using the processId as a parameter, continuing until you eventually receive the result once the process is complete.

like image 4
Steve Land Avatar answered Nov 11 '22 18:11

Steve Land