Is it safe to return a Stream in WebAPI?

2020-06-10T15:13:06

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.

Copyright License:
Author:「Pietro」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/62297799/is-it-safe-to-return-a-stream-in-webapi

About “Is it safe to return a Stream in WebAPI?” questions

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...
Are Java 8 Streams safe return types for public methods, in that it would be impossible to mutate the underlying object given a stream from it? For example, if I have a List and return list.stream...
My model class looks like this public Stream FileStream { get; set; } public string FileName { get; set; } public string ContentType { get; set; } Is it possible to return this model to webapi?
I'm trying to stream data (log messages) from a WebAPI endpoint. I'm using PushStreamContent for this purpose as suggested by others. However, streaming doesn't work, what I see is that the headers...
I have a webapi and I want to make my logic inside this controller thread safe. I want user can only update payroll when the last one updated and two update at the same time should not be happend....
I have setup a SQL database with FILESTREAM support and am trying to stream files retrieved from the database with SqlFileStream through WebAPI to the browser. For some reason it does not work, bu...
I have an wrapper class that has a method that goes off and downloads a file from a web server and needs to return said file. The HttpWebResponse object returns a Stream for the body. Should I re...
I have a problem with .NET Core 1.0 and WebAPI. I wanted to do simple controller that will forward data stream coming into PUT method to another REST service without any need to store this data in ...
Am having a zip file which needs to be sent to Asp.Net WebAPI from frontend browser. My WebAPI accepts a stream as input. So how can I convert zip to stream in frontend(in javascript) and make a post
My MVC/webApi calculates a report to be returned in a file as a stream. The calculation takes quite some time so in order to speed up the user experience it's cached. When it's returned the stream is

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.