.net 4.8 HttpClient still fails after network is restored

61 Views Asked by At

Repro steps:

  1. Turn off network by disabling the network adapter
  2. Start a httplient request with a cancellation, sure the SendAsync task will be cancelled.
  3. Turn on network, redo step 2, keeps failing.

What I have tried:

  1. set cache to false
  2. add a random number as a a=xxx parameter to the end of the url.
  3. set ServicePointManager.DnsRefreshTimeout = 0
  4. 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?

0

There are 0 best solutions below