1

My WebAPI needs to return a Stream.

using (var stream = new MemoryStream())
{
    //Fill the Stream
    stream.Seek(0, SeekOrigin.Begin);
    return new OkObjectResult(stream);
}

The above would not work as stream is disposed before the Client can get it. So I simply changed it to:

var stream = new MemoryStream();
//Fill the Stream
stream.Seek(0, SeekOrigin.Begin);
return new OkObjectResult(stream);

I wonder if it is safe to return a stream from a WebAPI. My doubt is that the garbage collector could dispose stream before the client has finished to get/download the content.

Pietro
  • 751
  • 7
  • 22

0 Answers0