C# Net.HttpClient Cancel ReadAsStringAsync?
No, you can't. There's no overload of ReadAsStringAsync
that accepts a cancellation token and you can't cancel a non-cancelable async operation.
You can however abandon that operation and move on with a WithCancellation
extension method, which won't actually cancel the operation but will let the code flow as if it has been:
static Task<T> WithCancellation<T>(this Task<T> task, CancellationToken cancellationToken)
{
return task.IsCompleted
? task
: task.ContinueWith(
completedTask => completedTask.GetAwaiter().GetResult(),
cancellationToken,
TaskContinuationOptions.ExecuteSynchronously,
TaskScheduler.Default);
}