Click here to Skip to main content
14,330,512 members
Rate this:
Please Sign up or sign in to vote.
See more:
In my frontend I have a button which calls export method for Excel file generation. It worked fine up until now, but recently I stumbled upon a problem that it doesn't work with big dataset. The whole excel generation takes awhile and after it finishes I get:

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.HttpListenerResponse' at System.Net.HttpListenerResponse.CheckDisposed() at System.Net.HttpListenerResponse.set_ContentLength64(Int64 value) at Microsoft.Owin.Host.HttpListener.RequestProcessing.HeadersDictionaryBase.S et(String key, String[] value)


I can't figure out why it works for smaller files and breaks for big one:

I can't add the whole code but maybe someone would help me out:

my exportController.cs method and the FormatXlsStream method that is called inside:

[HttpGet]
        [Route("Export")]
        public HttpResponseMessage ExportChart([FromUri(Name = "from")]long fromJs, [FromUri(Name = "to")]long toJs)
        {
         // ........
            // Fetch output stream, depending on the formatter desired
            var result = this.Request.CreateResponse(HttpStatusCode.OK);
           // .....
            var data = ServerModule.Exporter.FetchData();
            System.IO.MemoryStream ms;
            switch (format)
            {
                case "xls":
                    ms = ServerModule.Exporter.FormatXlsStream(data, headers.ToArray(), null, translator, filename, timezone, dateTimeMode, interval);
                    break;
// .....
            }
            result.Content = new StreamContent(ms);
            result.Content.Headers.Add("Content-Type", "application/octet-stream");
            result.Content.Headers.Add("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            if (download_token != null)
            {
                CookieHeaderValue chv = new CookieHeaderValue("token", download_token);
                chv.Path = "/";
                result.Headers.AddCookies(new CookieHeaderValue[] { chv });
            }
            return result;
        }
        
        
namespace Export
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using NPOI.HSSF.UserModel;
    using NPOI.SS.UserModel;

    public partial class Exporter
    {
        public System.IO.MemoryStream FormatXlsStream(Dictionary<DateTime, Data[]> sourceData, ExportHeader[] headers, Style documentStyle, Translator translator, string title, Timezone timezone, int? dateTimeMode, Interval reportInterval)
        {
            if (documentStyle == null)
            {
                documentStyle = this.defaultStyle;
            }
            // Create Workbook, either XLSX or XLS
            IWorkbook wb = (IWorkbook)new HSSFWorkbook();
// ..........
            // Save workbook into memory stream and rewind it
            System.IO.MemoryStream outputStream;
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                wb.Write(ms);
                outputStream = new System.IO.MemoryStream(ms.ToArray());
            }
            outputStream.Position = 0;
            return outputStream;
        }
    }
}


What I have tried:

Can the problem lie somewhere in MemoryStream using?
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100