question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add Results.Stream overload that takes a Func<Stream, Task>

See original GitHub issue

Background 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:open
  • Created 2 years ago
  • Reactions:4
  • Comments:13 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
pranavkmcommented, Jan 8, 2022
- Func<Stream, Task> streamFactory
+Func<Stream, Task> writeHttpResponseStreamAsync,

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)

1reaction
rafikiassumani-msftcommented, Jun 23, 2022

Triage: We will keep this issue open to give us time to add these results to Web APIs in MVC.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found