简体   繁体   中英

ASP.net API 500 Internal Error When Using Async Await When the Process is still going

I have a web API controller built on async await patter which calls external APIs, which takes 1 hour at times, the API is returning 500 Internal Server Error with Content-lenght Message. But the processing is going on in the background successfully, how do i avoid the 500 error and have it wait as long as the response comes, On the client side i have configure httpclient timeout to 2 hours. This is how my controller looks, any help is appreciated.

        public async Task<IActionResult> ProcessTestChange(string date, int ID)
        {
            try
            {
                var result = await _bckendService.ProcessTestChange(date, ID);
                var response = EncapsulateResult(result, result == null ? System.Net.HttpStatusCode.NotFound : result == 1 ? System.Net.HttpStatusCode.OK : System.Net.HttpStatusCode.InternalServerError, string.Empty);
                
                return Ok(response);
            }
            catch (Exception ex)
            {
                _logger.TrackException(ex);
                return new StatusCodeResult(StatusCodes.Status500InternalServerError);
            }
        }```

You can do the following to keep a HttpClient connection alive:

    System.Net.ServicePoint servicePoint = System.Net.ServicePointManager.FindServicePoint(url);
    if (servicePoint != null) servicePoint.SetTcpKeepAlive(true, 60000, 60000);

see also: https://docs.microsoft.com/en-us/dotnet/api/system.net.servicepointmanager.settcpkeepalive?view=net-5.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM