0
votes

For some web API requests, probably those long running, in an App Service hosted in Azure, I am getting response 502 error page. Sometimes the API call fails, sometimes it succeeds.

502 - Web server received an invalid response while acting as a gateway or proxy server

There is a problem with the page you are looking for, and it cannot be displayed. When the Web server (while acting as a gateway or proxy) contacted the upstream content server, it received an invalid response from the content server.

I tried to log into log files (LogFiles/eventlog.xml), but could not trace the error.

I suppose it is connected to some timeout, but do not know where it can be set in App Service's setting.

2

2 Answers

1
votes

It can be some issue with the network. I recommend you the implementation of the retry and circuit breaker patterns to better handle this kind of problems:

https://docs.microsoft.com/en-us/azure/architecture/patterns/retry

0
votes

In Azure, IIS is reverse proxy for Kestrel server.

I solved the problem by adding .UseKestrel(...) to the BuildWebHost function in Program.cs file as follows:

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

The more generic question about timeouts of long running tasks is answered here.