ERR_EMPTY_RESPONSE使用C#处理在ASP.Net大量文件时, [英] ERR_EMPTY_RESPONSE when processing a large number of files in ASP.Net using C#

查看:145
本文介绍了ERR_EMPTY_RESPONSE使用C#处理在ASP.Net大量文件时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个内联网系统来处理目录中的TXT文件,它的数据处理成一个SQL Server数据库。这是递归的,所以我有这样的文件结构:

C:\\ RAWDATA \\ 2012_06_01 \\ 2012_06_01_0002144493.txt
C:\\ RAWDATA \\ 2012_06_01 \\ 2012_06_01_0002954412.txt

...

C:\\ RAWDATA \\ 2012_06_15 \\ 2012_06_15_0012554778.txt

等等,总结高达约10.530文件。

选择只是一些文件夹(如从一个月的所有文件夹)时,它完美。
但是,试图处理的时候,让我们说,文件的年(从2011_06_01到2012_06_01),它开始处理,然后我得到这个ERR_EMPTY_RESPONSE错误。
它不连接相关的,因为它发生VS2010引擎下(我只是运行code,它会打开浏览器,我选择我想要的所有文件夹 - 在Web界面 - 单击进程按钮)。

的处理是由下循环做循环,这样的样品进行:

 的String []文件夹= Directory.GetDirectories(parentFolder);
的foreach(文件夹中的文件夹弦)
{
    字符串[] =文件f.GetFilesRecursive(@folder);
    的foreach(在文件中字符串的文件)
    {
        如果(File.Exists(文件))
        {
            字符串缓冲区=;
            StreamReader的SR = File.OpenText(文件);
            而(!sr.EndOfStream)
            {
                串线= sr.ReadLine();
                缓冲区+ =行;                //所有内部文件处理和数据库调用放在这里            } //而
            sr.Close();
        } //如果文件存在
    } //的foreach(文件)
} //的foreach(文件夹)

这code是原来的code的简化版本。内部文件处理code是非常大的,包括类,数据库对象等,但他们工作的罚款(含几个文件,最多300个文件左右,它的作品完美)。

有没有办法避免这种错误,或者对其他方法来优化文件处理任何建议?

同时,(最低重要性),我想实现一个Ajax响应,因此用户可以看到一个个酒吧或类似的东西...我听说过ICallbackEventHandler和IPostBackEventHandler,但我在不知道在这样的一个我使用循环如何实现这一点。

什么真的会AP preciated。

修改ON 2012-06-21:

我找到了答案。
通过@BumbleBee提出的解决方案给了我一个起点为研究,因为它返回的消息:


  

异步操作在此上下文中允许的。页开始异步操作必须具有异步属性设置为true,并且异步操作只能前preRenderComplete活动页面上进行启动。


发现这一点: ASP.NET真正的异步操作
其中有在回答帖子的链接上的溶液(<一个href=\"http://stackoverflow.com/questions/6962877/threadpool-queueuserworkitem-uses-asp-net\">ThreadPool.QueueUserWorkItem使用ASP.Net )

现在,我只需要找到一个方法来发送页面上使用AJAX 3的div数据。


解决方案

您可能会想改变的requestTimeout看看。

 &LT;的httpRuntime executionTimeout =一些大的价值在这里/&GT;

检查的这个一个例子

但是,这实际上不是在真正解决您的问题。你需要启动一个后台线程,并让它处理大处理,让一个消息给用户的UI线程的回报。 <一href=\"http://www.dotnetfunda.com/articles/article613-background-processes-in-asp-net-web-applications.aspx\"相对=nofollow>这里是一个例子,你如何能做到这一点。

I created an intranet system to handle TXT files in a directory and process it's data into a SQL Server database. It's recursive, so I have a file structure like this:

C:\RawData\2012_06_01\2012_06_01_0002144493.txt C:\RawData\2012_06_01\2012_06_01_0002954412.txt

...

C:\RawData\2012_06_15\2012_06_15_0012554778.txt

And so on, summing up to about 10.530 files.

It works perfectly when selecting just some folders (like all folders from a month). However, when trying to process, let's say, an year of files (from 2011_06_01 to 2012_06_01), it begins processing, and then I get this ERR_EMPTY_RESPONSE error. It is not connection related, since it happens under VS2010 engine (I just run the code, it opens the browser, I select all folders I want - on the web interface - and click "process" button).

The processing is being made by doing loops under loops, like this sample:

string[] folders = Directory.GetDirectories(parentFolder);
foreach (string folder in folders)
{
    string[] files = f.GetFilesRecursive(@folder);
    foreach (string file in files)
    {
        if (File.Exists(file))
        {
            string buffer = "";
            StreamReader sr = File.OpenText(file);
            while (!sr.EndOfStream)
            {
                string line = sr.ReadLine();
                buffer += line;

                // All the internal file processing and database calls goes here

            } // while
            sr.Close();
        } // if file exists
    } // foreach (files)
} // foreach (folders)

This code is a simplified version of the original code. The internal file processing code is really big, involves classes, database objects, etc. but they're working fine (with few files, up to 300 files or so, it works flawlessly).

Is there a way to avoid this error, or any suggestion about other ways to optimize file processing?

Also (and of minimum importance), I would like to implement an ajax response, so the user could see a percentage bar or something like that... I've heard about ICallbackEventHandler and IPostBackEventHandler, but I have no idea on how implement this under loops like this one I use.

Anything would really be appreciated.

EDIT ON 2012-06-21:

I found the answer. The solution suggested by @BumbleBee gave me a start point for researching, since it returned the message:

Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event.

Found this: ASP.NET true asynchronous operation which has the solution on a link in the answer post ( ThreadPool.QueueUserWorkItem uses ASP.Net )

Now, I just need to find a way to send data using AJAX to 3 Divs on page.

解决方案

You might want to change the RequestTimeout and see.

<httpRuntime executionTimeout="some big value here"  />

Check this for an example

But that actually isn't the real fix for your problem. You need to fire up a background thread and let it handle the big processing and let the UI thread return with a message to the user. Here is one example how you could achieve that.

这篇关于ERR_EMPTY_RESPONSE使用C#处理在ASP.Net大量文件时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆