Why am I getting "Cannot access a closed Stream" here?

44,605

Solution 1

The stream was closed as soon as you exited the action method, or rather, the using ( var ms = new MemoryStream() ) { block.

You don't need to dispose the MemoryStream. The FileStreamResult object returned by File(ms, "application/pdf", "Test.pdf"); will dispose it after rendering. The code that actually sends the stream data is :

protected async override Task WriteFileAsync(HttpResponse response)
{
    var outputStream = response.Body;

    using (FileStream)
    {
        var bufferingFeature = response.HttpContext.Features.Get<IHttpBufferingFeature>();
        bufferingFeature?.DisableResponseBuffering();

        await FileStream.CopyToAsync(outputStream, BufferSize);
    }
}

You can replace this using block with :

var ms = new MemoryStream();
try
{
     //..
     //From Igor's comment. FileStreamResult won't reset the stream position itself
     ms.Position=0;
     return File(ms, "application/pdf", "Test.pdf");
}
catch
{
    ms.Dispose();
    throw;
}

to ensure that the stream gets disposed if an error occurs.

UPDATE

As Igor mentioned, and as the source code shows, FileStreamResult won't reset the stream position. You'll have to set it to 0 before calling return File(...)

Solution 2

The PdfWriter class may be closing your stream. Make sure to set the CloseStream property to false.

Next you should not use using on the MemoryStream here, since the FileStreamResult action result will take care of disposing the stream after sending it off. Right now the stream is actually closed (by dispose) before the send takes place.

Also, you should seek the stream back to position 0 before sending off the file.

You can wrap the whole part in a try...catch however to dispose the stream in the case of an error (but the GC would eventually take care of it and MemoryStream if sully managed, so that's not mandatory).

Share:
44,605
Subpar Web Dev
Author by

Subpar Web Dev

Updated on March 24, 2020

Comments

  • Subpar Web Dev
    Subpar Web Dev over 4 years

    Stack trace looks like

    [ObjectDisposedException: Cannot access a closed Stream.]
    System.IO.__Error.StreamIsClosed() +53
    System.IO.MemoryStream.Read(Byte[] buffer, Int32 offset, Int32 count) +11411219 System.Web.Mvc.FileStreamResult.WriteFile(HttpResponseBase response) +81 System.Web.Mvc.FileResult.ExecuteResult(ControllerContext context) +168
    System.Web.Mvc.ControllerActionInvoker.InvokeActionResult(ControllerContext controllerContext, ActionResult actionResult) +13

    after invoking

            //Byte[] bytes;
            using ( var ms = new MemoryStream() )
            {
                using ( var doc = new Document() )
                {
                    using ( var writer = PdfWriter.GetInstance(doc, ms) )
                    {
    
                        doc.Open();
                        //var example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
                        var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "template.html"));
                        var example_css = @".headline{font-size:200%}";
                        using ( var srHtml = new StringReader(example_html) )
                        {
                            iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        }
                        using ( var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)) )
                        {
                            using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
                            {
                                iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                            }
                        }
    
    
                        doc.Close();
                    }
                }
                //bytes = ms.ToArray();
                return File(ms, "application/pdf", "Test.pdf");
            }
    

    I've read MemoryStream - Cannot access a closed Stream, but that's not the same scenario because I'm not using StreamReader

    Edit: Still not working with

        [OutputCache(NoStore = true, Duration = 0)]
        public ActionResult Run()
        {
            Byte[] bytes;
            var ms = new MemoryStream();
            try
            {
                using (var doc = new Document())
                {
                    using (var writer = PdfWriter.GetInstance(doc, ms))
                    {
                        writer.CloseStream = false;
                        doc.Open();
                        var example_html = @"<p>This <em>is </em><span class=""headline"" style=""text-decoration: underline;"">some</span> <strong>sample <em> text</em></strong><span style=""color: red;"">!!!</span></p>";
                        //var example_html = System.IO.File.ReadAllText(Path.Combine(Server.MapPath("~/EmailTemplates"), "LinkEmailTemplate.html"));
                        var example_css = @".headline{font-size:200%}";
                        using (var msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_css)))
                        {
                            using (var msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(example_html)))
                            {
                                iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                            }
                        }
                        doc.Close();
                    }
                }
                bytes = ms.ToArray();
                ms.Position = 0;
                return File(ms, "application/pdf", "Test.pdf");
            }
            catch
            {
                ms.Dispose();
                throw;
            }           
        }