11

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" />
ProgSky
  • 2,530
  • 8
  • 39
  • 65
  • Possible duplicate of [Can you set request timeout in asp.net core 2.0 hosted in IIS from C# code?](https://stackoverflow.com/questions/50210876/can-you-set-request-timeout-in-asp-net-core-2-0-hosted-in-iis-from-c-sharp-code) – Peter Bons Nov 28 '18 at 20:00
  • https://stackoverflow.com/questions/2414441/how-to-increase-request-timeout-in-iis – Gina Marano Nov 28 '18 at 20:02
  • does this iis site sit behind a load balancer? you can set timeout thresholds at the load balancer as well which could be your problem. suggesting this because this same thing had me banging my head against a wall not long ago. places to check: application code timeout, iis timeout, load balancer timeout – GregH Nov 28 '18 at 20:15
  • @GregH thanks for the suggestion. I will check the load balancer part. – ProgSky Nov 28 '18 at 21:15
  • @ginalster Yeah, I changed that to 10 mins, it has no impact. – ProgSky Nov 28 '18 at 21:16
  • Do you really have a request timeout and not a database timeout? – PmanAce Nov 28 '18 at 21:24
  • @PmanAce I wish it was database timeout. It is definitely request timeout. and Its 2mins, I checked it multiple times. anything under 2 mins go through fine. This is what I am trying to overwrite – ProgSky Nov 28 '18 at 21:51

3 Answers3

14

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

ProgSky
  • 2,530
  • 8
  • 39
  • 65
4

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.

Steve Land
  • 4,852
  • 2
  • 17
  • 36
  • Yes. Thats exactly I was thinking to do. Btw, I was able to increase timeout by adding web.config and then added requestTimeout="00:20:00" – ProgSky Nov 28 '18 at 22:47
0

It may be Postman that is timing out. If that is the case you can adjust the Postman settings.

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

File -> Settings -> General : Request Timeout

It may default to infinity already, don't know if I changed it manually.

Just a thought.

Gina

Gina Marano
  • 1,773
  • 4
  • 22
  • 42
  • Thanks. But not really. My Request timeout is set to 0 for Infinity. Also, My Angular UI also times out when I call the api end point. Somewhere this 2 min is set that I am unable to override. sigh. – ProgSky Nov 28 '18 at 19:58