Repro steps:
- Turn off network by disabling the network adapter
- Start a httplient request with a cancellation, sure the SendAsync task will be cancelled.
- Turn on network, redo step 2, keeps failing.
What I have tried:
- set cache to false
- add a random number as a a=xxx parameter to the end of the url.
- set
ServicePointManager.DnsRefreshTimeout = 0 - create HttpClient with
IHttpClientFactory
I am using .net 4.8 with just the simple HttpClient code as below:
var options = new JsonSerializerOptions
{
ReadCommentHandling = JsonCommentHandling.Skip,
AllowTrailingCommas = true,
};
var cts = new CancellationTokenSource();
cts.CancelAfter(TimeSpan.FromSeconds(15));
HttpClient client = new HttpClient();
await httpClient.GetFromJsonAsync(url, typeof(MyType), options, cancellationToken) as MyType;
or
await httpClientFactory.CreateClient().GetFromJsonAsync(url, typeof(MyType), options, cancellationToken) as MyType;
It keeps failing with this error:
A task was canceled
and the InnerException is null. Any ideas?