1

I have a REST controller that streams the response in csv format using a helper method like below:

public static void CsvStreamHelper(IEnumerable<T> data, Stream stream)
{
    using (var writer = new StreamWriter(stream))
    {
        foreach (var line in data)
        {
            // format csv lines here
            writer.WriteLine(lineString);
        }

        writer.Flush();
    }
}

Then, I'm using this in my Controller like:

public Task<IActionResult> MyController()
{
    var data = // Get data here.

    CsvStreamHelper(data, this.HttpContext.Response.Body);

    return new EmptyResult();
}

This is working fine. However, I would like to use content negotiation middleware with custom formatter like here while continue to stream response.

I can override WriteResponseBodyAsync method using my helper method. What I'm unsure about is if I use it in my Rest controller like this.Ok(data), instead of streaming the response, it will just build the response and send it in one chunk. How can I achieve streaming response with content negotiation middleware?

kovac
  • 4,945
  • 9
  • 47
  • 90
  • Have you tried options like System.Net.Http.StreamContent and System.Net.Http.PushStreamContent? http://blog.guvweb.co.uk/2014/07/02/streaming-web-api/ – Deepak Agarwal Jan 23 '19 at 04:25
  • Both these APIs seem to be old... I've used PushStreamContent in .Net Framework. I don't think it's the way to go with .Net Core. – kovac Jan 23 '19 at 14:02

0 Answers0