Share via

API Threading issue - Concurrent connection cancelling the API Thread

2021-10-14T10:41:35.773+00:00

Hi Team,

We are using ASP.NET Core 3.1 and making API calls using httpclient with "GetASync" and "PostAsync" methods. During concurrent calls, the API thread is getting terminated / cancelled with below exception. Could you please help to know how to handle this error and resolve this issue for concurrent user calls?

Code Sample:

var clientt = new HttpClient();

client.Timeout = Timespan.FromSeconds (120);

clientt.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

var response = clientt.PostAsync(uri, content).Result;

response.Wait();

string resultString = response.Content.ReadAsStringASync().result;

Exception:
at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions) at System.Threading.Tasks.Task.Wait(Int32 millisecondsTimeout, CancellationToken cancellationToken) at System.Threading.Tasks.Task.Wait()

Developer technologies | ASP.NET Core | Other

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 84,071 Reputation points
    2021-10-14T16:18:53.857+00:00

    also the error indicates the 120 second timeout fired and canceled the task. maybe the site you are calling is as poorly written as your sample code.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.