sooqua sooqua - 2 months ago 19
C# Question

SendAsync with CancellationToken

I was looking through the code of my old project and I found this:

private async void send(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpResponseMessage response = await _client.SendAsync(request, cancellationToken);
//...
}


I was wondering what would happen when it's cancelled?

What would be in the response?

Do I need to call
ThrowIfCancellationRequested
after that, or it will raise an exception itself?

Answer

I was wondering what would happen when it's cancelled?

The standard pattern is to cancel the returned task. This will cause your await to raise an OperationCanceledException.

What would be in the response?

There would be no response object, since the exception is thrown before that point.

Comments