Add Results.Stream overload that takes a Func<Stream, Task>
See original GitHub issueBackground and Motivation
There are scenarios where APIs want to work directly on the response stream without buffering. Today the Stream overloads assume you have a stream that you have produced that is then copied to the underlying response stream, instead, we want to provide a result type that gives you access to the underlying response stream.
Example:
https://github.com/khalidabuhakmeh/dotnet-dramameter/blob/main/DramaMeter/Program.cs#L37-L43
Another is writing a blob storage results to the http response.
Proposed API
namespace Microsoft.AspNetCore.Http
{
public static class Results
{
+ public IResult Stream(Func<Stream, Task> streamWriterCallback,
+ string? contentType = null,
+ string? fileDownloadName = null,
+ DateTimeOffset? lastModified = null,
+ EntityTagHeaderValue? entityTag = null,
+ bool enableRangeProcessing = false);
}
}
Usage Examples
app.MapGet("/", async (HttpContext http, string? level) => {
level ??= "low";
if (!levels.TryGetValue(level.Trim(), out var result))
return Results.BadRequest("invalid level");
var image = background.CloneAs<Rgba32>();
image.Mutate(ctx => {
ctx.Vignette(result.color); // match the background to the intensity
ctx.DrawImage(foreground, new Point(0, 0), 1f);
ctx.DrawImage(result.image, new Point(0, 0), opacity: 1f);
});
http.Response.Headers.CacheControl = $"public,max-age={FromHours(24).TotalSeconds}";
return Results.Stream(stream => image.SaveAsync(stream, PngFormat.Instance), "image/png");
});
Risks
None
Issue Analytics
- State:
- Created 2 years ago
- Reactions:4
- Comments:13 (12 by maintainers)
Top Results From Across the Web
c# - How to process data from one stream and return it as ...
What that method needs to do is: Immediately return the output stream; Keep loading input stream data in chunks (for example 32 bytes);...
Read more >Implementing the Task-based Asynchronous Pattern
Use the overloads of the Task.ContinueWith method. This method creates a new task that is scheduled when another task completes. Some of the ......
Read more >Lambda function handler in C# - AWS ...
The Lambda function handler is the method in your function code that processes events. When your function is invoked, Lambda runs the handler...
Read more >How to stream data directly from the database through a 3 ...
The solution: I've managed to write something that will stream directly ... async Task<HttpResponseMessage> GetBackEndWebApi() { var results ...
Read more >Usage | RestSharp
Essentially, RestSharp is a wrapper around HttpClient that allows you to do the following: Add default parameters of any kind (not just headers) ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
or something like that. I think we’d spoken about this API when reviewing the
Results
type but left it alone at that time to get feedback. Also I think we should try and keep some parity between this and the API in controllers to allow users to migrate from one to another without too much effort (in particular since it’s low cost for us to add support)Triage: We will keep this issue open to give us time to add these results to Web APIs in MVC.