简体   繁体   English

ASP System.OutOfMemoryException仅在Visual Studio中捕获,不在生产服务器上

[英]ASP System.OutOfMemoryException only caught in Visual Studio not on production server

I have written a method to export the contents of an asp GridView object to Excel with formatting. 我编写了一种使用格式将asp GridView对象的内容导出到Excel的方法。 On the very rare, but completely repeatable occasion where that particular GridView has a huge number of rows (24k) the method fails and gets caught and handled accordingly. 在非常罕见但完全可重复的情况下,该特定GridView具有大量行(24k),该方法将失败并被相应地捕获和处理。

This works perfectly in Visual Studio (2015) but not when moved to the production server, which instead throws a System.OutOfMemoryException. 这在Visual Studio(2015)中完美运行,但在移至生产服务器时则无效,后者转而抛出System.OutOfMemoryException。

Method: 方法:

protected void ExportToExcel(object sender, EventArgs e)
{
    Button clickedButton = (Button)sender;
    GridView selectedGridview = GridView1;
    string filename = "FileNameHere";
    // Hidden unimportant selection of gridview
    if (selectedGridview.Rows.Count > 0)
    {
        try
        {
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=" + filename + ".xls");
            Response.Charset = "";
            Response.ContentType = "application/vnd.ms-excel";
            using (StringWriter sw = new StringWriter())
            {
                HtmlTextWriter hw = new HtmlTextWriter(sw);

                //To Export all pages
                selectedGridview.AllowPaging = false;

                foreach (TableCell cell in selectedGridview.HeaderRow.Cells)
                {
                    cell.BackColor = selectedGridview.HeaderStyle.BackColor;
                }
                foreach (GridViewRow row in selectedGridview.Rows)
                {
                    foreach (TableCell cell in row.Cells)
                    {
                        if (row.RowIndex % 2 == 0)
                        {
                            cell.BackColor = selectedGridview.AlternatingRowStyle.BackColor;
                        }
                        else
                        {
                            cell.BackColor = selectedGridview.RowStyle.BackColor;
                        }
                        cell.CssClass = "textmode";
                        cell.Attributes.Add("style", "mso-number-format:\\@");
                    }
                }
                selectedGridview.RenderControl(hw);

                //style to format numbers to string
                string style = @"<style> .textmode {mso-number-format:\\@;} </style>";
                Response.Write(style);
                Response.Output.Write(sw.ToString());
            }
        }
        catch(System.OutOfMemoryException OOMex)
        {
            Response.Clear();
            // Another method gets called here to instead create a CSV file (much lighter on memory)
        }
        finally
        {
            Response.Flush();
            Response.End();
        }
    }

Any ideas as to why it works locally but throws the exception on production? 关于为什么它在本地工作但在生产中引发异常的任何想法?

Any help would be greatly appreciated. 任何帮助将不胜感激。

UPDATE: 更新:
I have since moved over to Mike Gledhill's library which drastically decreases complexity, but still raises the same error. 从那以后,我移到了Mike Gledhill的库中,该库大大降低了复杂性,但仍然会引发相同的错误。

After discussing solutions with him, I had already set <httpRuntime maxRequestLength="1000000000" executionTimeout="7200"/> and proceeded to test by ignoring the session variables and directly exporting the DataTable to Excel. 与他讨论解决方案后,我已经设置了<httpRuntime maxRequestLength="1000000000" executionTimeout="7200"/> ,并通过忽略会话变量并将DataTable直接导出到Excel进行了测试。 This works the same with the only difference being that now on the large result sets, I get System.IO.IsolatedStorage.IsolatedStorageException: Unable to create the store directory . 这是相同的,唯一的区别是现在在大型结果集上,我得到System.IO.IsolatedStorage.IsolatedStorageException: Unable to create the store directory I am consulting with the server manager at the moment to rectify this. 我目前正在咨询服务器管理员以纠正此问题。

UPDATE 2: 更新2:
I followed the following instructions to resolve the System.IO.IsolatedStorage.IsolatedStorageException : MSDN Forum 我按照以下说明解决了System.IO.IsolatedStorage.IsolatedStorageExceptionMSDN论坛
Everything is now working 100%! 现在一切都正常工作了100%!

Bottom Line: 底线:
I'd still like to know how or why an exception could get past my try/catch. 我仍然想知道异常如何或为什么会超出我的尝试/捕获范围。 Is it possible that the server won't run a catch clause if it is out of addressable/allocatable memory? 如果服务器的可寻址/可分配内存不足,是否有可能不会运行catch子句? Perhaps it "runs out" of memory in the catch clause and cannot get re-caught? 也许它在catch子句中的内存“用完了”并且无法重新捕获?

I can't exactly tell you why that exception is occurring, but if you're going to be writing particularly large Excel files, I would recommend doing it properly, using OpenXML , and the OpenXmlWriter library. 我无法确切告诉您为什么会发生该异常,但是如果您要编写特别大的Excel文件,我建议您使用OpenXMLOpenXmlWriter库正确执行。

Here's a free C# library (with source code) which will take a DataSet , DataTable or List<> and create a real Excel .xlsx file. 这是一个免费的C#库(带有源代码),它将使用DataSetDataTableList<>并创建一个真实的Excel .xlsx文件。

Export to Excel class 导出到Excel类

Simply pass it whatever data source you used to populate your GridView , eg: 只需将用于填充GridView任何数据源传递给它即可,例如:

CreateExcelFile.CreateExcelDocument(myDataSet, "YourFilename.xlsx");

And if you're attempting to create the Excel file on a website, you just need to pass it the HttpResponse : 而且,如果您尝试在网站上创建Excel文件,则只需将HttpResponse传递给它即可:

CreateExcelFile.CreateExcelDocument(myDataSet, "YourFilename.xlsx", Response);

But yes, you will need to modify this code, to add your Excel formatting. 但是,是的,您将需要修改此代码以添加Excel格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM