Is it safe to return a FileStream in a method in C#?
asp.net, asp.net-web-api, c#
Solution
You should not be returning a file stream but instead an array of bytes. This way you can correctly dispose of the object correctly and not worry about other calling methods in the stack.
byte[] currentFile = ....
You can then deliver your file as follows this, a byte array is easy to convert to anything. Below example is for MVC4.
return new FileContentResult(currentFile, "application/pdf");
Problem
In ASP.NET WebForms 4.5, I have WebAPI Controller with a GET method for getting a PDF. Then down in the business layer of the application, I have an API class with a method that contains the logic for actually finding and returning the PDF to the controller. So MyController class basically has: ``` public HttpResponseMessage GetStatement(string acctNumber, string stmtDate) { MyApi myApi = new MyApi(); HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); FileStream stream = myApi.GetStatement(acctNumber, stmtDate); ...set the response.Content = stream... ... set the mime type.. ... close the stream... return response; } ``` And MyApi class has: ``` public FileStream GetStatement(string acctNumber, string stmtDate) { ... makes an HttpWebRequest to get a PDF from another system ... HttpWebRequest req = WebRequest.Create(......).... FileStream stream = new FileStream(accountNumber +"_" + stmtDate + ".pdf", FileMode.Create); response.GetResponseStream().CopyTo(stream); return stream; } ``` The API class is not in the web layer of the application because it's used by other (non-web) parts of the software. I guess my concern is there's no explicit closing of the FileStream in the API method. I could do it in the Controller method, but I'd be relying on others to do the same when they're calling it from other areas. Is there a better way to return the PDF file from the API method? Possibly just as a byte array or something like that? Preferably as little overhead as possible. Thanks-