Skip to content

HttpContent.ReadAsStream sync API  #37495

@ManickaP

Description

@ManickaP

Background and Motivation

We introduced HttpClient sync API in #34948. However, the access to response's HttpContent is still purely asynchronous (e.g. ReadAsStreamAsync). To fully support fully a synchronous flow we should also support synchronous read of HttpContent.

Proposed API

Following minimalist approach from #32125 we should add sychronous method to retrieve Stream from HttpContent.

namespace System.Net.Http
{
    public abstract partial class HttpContent : System.IDisposable
    {
        // Existing methods:
        protected virtual System.Threading.Tasks.Task<System.IO.Stream> CreateContentReadStreamAsync();
        protected virtual System.Threading.Tasks.Task<System.IO.Stream> CreateContentReadStreamAsync(System.Threading.CancellationToken cancellationToken);

        public System.Threading.Tasks.Task<System.IO.Stream> ReadAsStreamAsync();
        public System.Threading.Tasks.Task<System.IO.Stream> ReadAsStreamAsync(System.Threading.CancellationToken cancellationToken);
        
        // Proposed new methods, following the same implementation pattern as ReadAsStreamAsync and CreateContentReadStreamAsync        
+       protected virtual System.IO.Stream CreateContentReadStream(System.Threading.CancellationToken cancellationToken = default);

+       public System.IO.Stream ReadAsStream();
+       public System.IO.Stream ReadAsStream(System.Threading.CancellationToken cancellationToken);
     }
}

Implementation Notes

All our own HttpContent implementations do have Stream readily available, see implementations of:

internal virtual Stream? TryCreateContentReadStream() => null;

Discussion Topics

  • CancellationToken support in ReadAsStream (include or not)?
  • Include CreateContentReadStream both overloads, one overload with default, or only one overload without default?

Prototype in #37494

cc: @dotnet/ncl @stephentoub

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions