Carter: Unable to respond with a Stream

I’m unable to work out how to reply with a streamed response using Botwin. Reading up on ASP.NET Core it looks liked I’d be able to do the following:

[HttpGet]
public FileStreamResult GetTest()
{
  var stream = new MemoryStream(Encoding.ASCII.GetBytes("Hello World"));
  return new FileStreamResult(stream, new MediaTypeHeaderValue("text/plain"))
  {
    FileDownloadName = "test.txt"
  };
}

(https://stackoverflow.com/questions/42771409/how-to-stream-with-asp-net-core#42772150)

However, I can’t work out how I can do this using the HttpResponse I have from the BotwinModule handler?

Here’s a snippet of my module, but it doesn’t return any data:

var models = this.journalReader.Read(start, end, type, ct);

var fileStream =
    new FileStreamResult(CreateExportStream(models, requestLog, ct), "application/csv")
        {
            FileDownloadName
                = $"journal-{type}.csv"
        };

res.Body = fileStream.FileStream;

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 23 (8 by maintainers)

Most upvoted comments

This works perfectly for streams which can be processed in memory, but unfortunately in my case, this doesn’t fix the issue. I’m using a producer/consumer pattern to minimise the memory requirements of my service - the data coming from the database could be many hundreds of MB and I’d rather stream it directly to the client.

This means, I’m not likely to have the entire contents of the stream in memory at once and is the cause of the block - StreamCopyOperation.CopyToAsync won’t start copying until the source stream has completed.

The solution (with and without hacky delay) doesn’t work for streams larger than my producer/consumer buffer size. 😦

Might not be able to, just a brain dump for now 😄