流处理之前EndOfData是真实的 [英] Stream is disposing before EndOfData is true

查看:179
本文介绍了流处理之前EndOfData是真实的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个连接到FTP的方法,读取csv文件到流然后使用TextFieldParser来处理数据。



一切正常,除了我得到,当它通过阅读CSV大约一半得到一个问题,突然我得到一个的ObjectDisposedException例外。我试图通过这两个StreamReader的&放大器;的TextReader将使用TextFieldParser但无论结果同样的问题。



我应该下载CSV到一个临时的本地目录,然后读取或者没有问题,读文件从FTP?我想可能有一些服务器设置可能超时流读取整个文件之前。

 的FtpWebRequest请求=(的FtpWebRequest) FtpWebRequest.Create(REMOVED.csv); 
request.Credentials =新的NetworkCredential(XYZ,*******);使用使用(流流= response.GetResponseStream())(WebResponse的响应= request.GetResponse())
{

{
使用(TextReader的读者
=新StreamReader的使用(流))
{
(TextFieldParser分析器=新的TextFieldParser(阅读器))
{
parser.HasFieldsEnclosedInQuotes = TRUE;
parser.Delimiters =新的字符串[] {,};
,而(!parser.EndOfData)//异常被抛出这里大约1500lines成CSV
{
Console.WriteLine(parser.ReadLine()的ToString());
}
}
}
}
}




输出最后行之前抛出的异常



190500,速递,货运商,分销商,1,5,15 / 12 / 2014年16:44




如果我应该下载该文件第一次,我只是用WebClient.DownloadFile()?我怎么能知道什么时候该文件已完成下载,以便然后读它



编辑:



系统净跟踪输出显示以下




详细的System.Net.Sockets 0:[10412]退出插槽#24914721: :接收() - >的Int32#95
System.Net信息:0:[10412] FtpControlStream#15315213 - 收到的响应[226文件传输成功
2265.748秒(此处测量),每千字节30.91秒]
System.Net信息:0:[10412]的FtpWebRequest#16868352::(释放FTP连接#15315213)




进一步编辑



从System.Net跟踪输出显示CSV的最后一行接收,为什么被设置在解析器在完成之前?我还是很新的节目,所以我真的不知道如何着手。


解决方案

所以我无法弄清楚如何停止流之前,我读给我TextFieldParser最终处置,而不是我做了以下内容:




 连接到FTP 
文件读入到一个流
流复制到一个FileStream,并创建一个临时文件
读取临时文件
删除临时文件




这是不是很优雅,但如果其他人遇到上述错误认识该下载文件是一个替代方法。


I am writing a method that connects to an FTP, reads a csv file into a stream then uses a TextFieldParser to process the data.

It's all working except for an issue I'm getting when it gets about halfway through reading the CSV suddenly I get an ObjectDisposedException exception. I've tried passing both the StreamReader & TextReader to the TextFieldParser but both result in the same problem.

Should I be downloading the CSV to a temporary local directory and then reading that or is there no issue reading a file from an FTP? I figured there might be some server setting possibly timing out the stream before it reads the entire file.

FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("REMOVED.csv");
request.Credentials = new NetworkCredential("xyz", "*******");
using (WebResponse response = request.GetResponse())
{
    using (Stream stream = response.GetResponseStream())
    {
        using (TextReader reader = new StreamReader(stream))
        {
            using (TextFieldParser parser = new TextFieldParser(reader))
            {
                parser.HasFieldsEnclosedInQuotes = true;
                parser.Delimiters = new string[] { "," };
                while (!parser.EndOfData) //exception is thrown here about 1500lines into csv
                {
                    Console.WriteLine(parser.ReadLine().ToString());
                }
            }
        }
    }
}

LAST LINE OF OUTPUT BEFORE EXCEPTION THROWN

190500,Courier Delivery,Freight,Distributor,1,5,15/12/2014 16:44

If I should be downloading the file first, do I just use WebClient.DownloadFile() ?? How can I tell when the file has finished downloading to then read it?

EDIT:

System net tracing output shows the following

System.Net.Sockets Verbose: 0 : [10412] Exiting Socket#24914721::Receive() -> Int32#95 System.Net Information: 0 : [10412] FtpControlStream#15315213 - Received response [226-File successfully transferred 226 5.748 seconds (measured here), 30.91 Kbytes per second] System.Net Information: 0 : [10412] FtpWebRequest#16868352::(Releasing FTP connection#15315213.)

FURTHER EDIT

The output from System.Net Tracing shows the last line of the CSV being received, so why is the parser being Disposed before it finishes?? I'm still quite new to programming so I'm not really sure how to proceed

解决方案

So I was unable to figure out how to stop the stream disposing before I read to the end with my TextFieldParser, instead I did the following:

Connect to FTP
Read the file into a stream
Copy the stream to a FileStream and create a temporary file
Read the temporary file
Delete the temporary file

This isn't very elegant but if anyone else comes across the above error know that downloading the file is an alternative.

这篇关于流处理之前EndOfData是真实的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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