Expose a TypedResults
method to push a partial response
#55753
Labels
api-suggestion
Early API idea and discussion, it is NOT ready for implementation
area-minimal
Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc
Background and Motivation
In a project I am developing I have the need to accept ranged requests and provide the client with partial responses (206).
The data for these responses is managed in a chunk-based store which is somewhat awkward to wrap in a
Stream
.For full responses, the
PushStreamHttpResult
is ideal, but it currently does not support partial requests.It is quite straightforward to implement an
IResult
that does what I need, but it looks like I would need to re-implementHttpResultsHelper.WriteResultAsFileCore
as it is aninternal
method.Proposed API
Usage Examples
Alternative Designs
The proposed API exposes the range information as
RangeItemHeaderValue?
.There are several alternatives to represent the range; this type was proposed based on what is used internally/emitted by
HttpResultsHelper.WriteResultAsFileCore
.The proposed API returns a
PushStreamHttpResult
, under the assumption that the same class can be used for both methods implementing responses-pushing-to-body.This would involve changes to
PushStreamHttpResult
; another approach would be to write a separateIResult
implementation inMicrosoft.AspNetCore.Http.HttpResults
.As mentioned in the background, it is possible to develop an independent
IResult
implementation; this requires duplicating the functionality implemented inHttpResultsHelper.WriteResultAsFileCore
.Another option is to wrap this as a
Stream
and use theTypedResults.Stream(Stream stream, ...)
overload, but this is inconvenient for two reasons:TypedResults.Stream(Func<Stream, Task> stream, ...)
)See https://gist.github.com/ranma42/a528555972f16c17ed1b840bfe7fbf5c for an example of the stream hack.
Risks
The API should be an extension of the existing API and AFAICT involves no breaking change.
I have experimented locally with an implementation that extends
PushStreamHttpResult
; in order to reuse the same class, some minor changes are needed that should not result in performance regressions.A straightforward implementation would simply replace the callback with a
Func<RangeItemHeaderValue?, HttpContext, Task>
and wrap theFunc<Stream, Task> streamWriterCallback
as(range, httpContext) => streamWriterCallback(httpContext.Response.Body)
.The text was updated successfully, but these errors were encountered: