Cancel HttpClient Does Not Kill TCP Base Call

I am trying to set the default timeout for my HttpClient calls to 5 seconds.

I made it through CancellationTokenSource.

Here is the corresponding bit of code:

var cancellationToken = new CancellationTokenSource();
cancellationToken.CancelAfter(TimeSpan.FromSeconds(5));
var result = _httpClient.SendAsync(request, cancellationToken.Token);

It works as I expected in terms of the calling code that received the "The task was canceled" error (I tested in the .NET 4.7 console application), but I noticed that in Fiddler the request still worked for 1 minute, until it finally refused:

enter image description here

Can anyone explain this behavior?

I expect that the base request will also be canceled when a cancellation is triggered.

_httpClient created as: new HttpClient { BaseAddress = baseAddress }

I know there is a parameter Timeout, but not sure if I should use these or invalidating tokens? My guess is Timeoutfor non-asynchronous / pending cases?

+4
1

, HttpClient , , .

HttpClient / . , , . , -, 1 , .

, 5 , Timeout _httpClient TimeSpan.FromSeconds(5). (a TaskCanceledException , 5 ).

+1

Source: https://habr.com/ru/post/1683147/


All Articles