开发者

MVC - dispose resource in controller

What's the proper way of disposing resources when a method in the controller returns filestream or file or filecontentresul开发者_如何学Got objects ?

For ex when doing something like below:

   using CrystalDecisions.CrystalReports.Engine;

public ActionResult Report()
{
    ReportClass rptH = new ReportClass();
    rptH.FileName = Server.MapPath("[reportName].rpt");
    rptH.Load();
    rptH.SetDataSource([datatable]);
    Stream stream = rptH.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
    return File(stream, "application/pdf");   
}

TIA


A FileStreamResult (which is what is created) wraps the stream in a using statement when WriteFile is called during result execution. This will close and dispose of the stream. Depending on the type of stream created it may actually cause an error to either close or wrap the usage in a using statement in the controller action.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜