Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HttpClient Task was cancelled

I am trying to use HttpClient to asynchronously log a message to a REST service using the code below:

public void LogMessage(string operationURI, string message, EventLogEntryType logEntryType)
        {
            using (var client = new HttpClient())
            {
                var cancellationToken = new CancellationToken();
                client.SendAsync(GetRequest(operationURI), cancellationToken).ContinueWith(
                    cw =>
                    {
                        var response = cw.Result; //(I get an error on this line)
                        if (!response.IsSuccessStatusCode)
                        {
                            LogMessageLocal(message, logEntryType);
                        }
                    });
            }
        }

Note: The GetRequestMessage returns an HttpRequestMessage.

But I get an error stating 'A task was canceled.'

Any ideas?

like image 647
Rema Manual Avatar asked Jun 13 '26 13:06

Rema Manual


1 Answers

I believe this can occur when the timeout is exceeded. You might check your timeout, and log how long it was outstanding before the exception to see if it is being exceeded.

like image 115
Andrew Arnott Avatar answered Jun 18 '26 01:06

Andrew Arnott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!