Closed
Description
Background and motivation
HttpContent.LoadIntoBufferAsync
and HttpContent.LoadIntoBufferAsync(long)
currently forward to overloads which take a cancellation token, passing CancellationToken.None
.
runtime/src/libraries/System.Net.Http/src/System/Net/Http/HttpContent.cs
Lines 476 to 477 in 5535e31
HttpClient
also uses this API but passes a cancellation token:
It would be useful for external consumers to be able to pass a cancellation token, similar to HttpContent.CopyToAsync(Stream, CancellationToken
API Proposal
namespace System.Net.Http;
public class HttpContent
{
public Task LoadIntoBufferAsync();
public Task LoadIntoBufferAsync(long maxBufferSize);
+ public Task LoadIntoBufferAsync(CancellationToken cancellationToken);
+ public Task LoadIntoBufferAsync(long maxBufferSize, CancellationToken cancellationToken);
}
API Usage
using var client = new HttpClient();
using var response = await client.GetAsync("https://example.com", HttpCompletionOption.ResponseHeadersRead);
using (var timeoutCts = new CancellationTokenSource(TimeSpan.FromSeconds(30))
{
var maxBufferSize = 134217728L;
await response.LoadIntoBufferAsync(maxBufferSize, timeoutCts.Token);
}
Alternative Designs
If the HttpContent
has been created by HttpClient
, it could internally track cancellation based on the token passed to SendAsync
. However I don't think this is consistent with existing HttpContent
methods like CopyToAsync
.
Risks
None