我有一个DocumentGenerator包装的课程MemoryStream.所以我已经IDisposable在课堂上实施了.
我看不出如何/在哪里可以处理它.
这是我当前的代码,它在MVC中执行文件下载:
using (DocumentGenerator dg = DocumentGenerator.OpenTemplate(path))
{
/* some document manipulation with the
DocumentGenerator goes here ...*/
return File(dg.GetDocumentStream(), "text/plain", filename);
}
Run Code Online (Sandbox Code Playgroud)
在控制器完成之前关闭/处理流时出现此错误.在这种情况下,如何确保我的资源得到妥善处理?
编辑:我现在的执行IDisposable只是处置MemoryStream.我知道这不是一个正确的实现,我只是用它作为测试.有什么不同我可以在这里做它的工作吗?
public void Dispose()
{
_ms.Dispose();
_ms = null;
}
Run Code Online (Sandbox Code Playgroud)