Is it safe to return a FileStream in a method in C #?

In ASP.NET WebForms 4.5, I have a WebAPI controller with a GET method to receive a PDF file.

Then in the business layer of the application, I have an API class with a method that contains the logic for actually searching and returning the PDF file to the controller.

So, the MyController class 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 the 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 at the web tier of the application because it is used by other (not web parts) software.

As far as I understand, I do not see the explicit closing of the FileStream in the API method. I could do this in the Controller method, but I would rely on others to do the same when they call it from other areas.

PDF API? , - ? .

-

+4
4

, - . , .

byte[] currentFile = ....

: . MVC4.

return new FileContentResult(currentFile, "application/pdf");
+7

, FileStream s, , using. , . Inversion of Control, , , , FileStream, using.

public T GetStatement<T>(string acctNumber, string stmtDate,
    Func<FileStream, T> callback) {
    ... makes an HttpWebRequest to get a PDF from another system ...
    HttpWebRequest req = WebRequest.Create(......)....
    using(FileStream stream = new FileStream(accountNumber +"_" + stmtDate + ".pdf", FileMode.Create))
    {
        response.GetResponseStream().CopyTo(stream);
        return callback(stream);
    }
}

, , , .

, , , . , , .

+3

, FileStream using, , , . , FileStream . :

public ActionResult GetImage()
{            
    Stream stream = //get the stream
    return base.File( stream, "image/png" ); 
}

, , . . .

+1

, , , .

public async Task<HttpResponseMessage> Get(string acctNumber, string stmtDate)
{
    HttpResponseMessage response2 = new HttpResponseMessage();

    HttpClient client= new HttpClient();
    string url = "http://localhost:9090/BusinessLayer?acctNumber=" + acctNumber + "&stmtDate=" + stmtDate;
    // NOTE 1: here we are only reading the response1 headers and not the body. The body of response1 would be later be 
    // read by response2.
    HttpResponseMessage response1 = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);

    // NOTE 2: here we need not close response1 stream as Web API would close it when disposing
    // response2 stream content.
    response2.Content = new StreamContent(await response1.Content.ReadAsStreamAsync());
    response2.Content.Headers.ContentLength = response1.Content.Headers.ContentLength;
    response2.Content.Headers.ContentType = response1.Content.Headers.ContentType;

    return response2;
}
+1

All Articles