Cannot access a disposed object. Object name: System.Net.Sockets.NetworkStream
Asked Answered
I

2

6

I am using CsvHelper. Here I am trying to download a csv file from ftp and write to a class. It's throwing an error - Cannot access a disposed object. Object name: System.Net.Sockets.NetworkStream. from line - IEnumerable records = csv.GetRecords().ToList();

Any idea?

            request.Credentials = new NetworkCredential(ftpUser, ftpPwd);
            request.Method = WebRequestMethods.Ftp.DownloadFile;
            request.UseBinary = true;

            // Csv file
            using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
            {
                using (Stream responseStream = response.GetResponseStream())
                {
                    if (responseStream != null)
                    {
                        using (TextReader tr = new StreamReader(responseStream))
                        {
                            using (CsvReader csv = new CsvReader(tr))
                            {
                                if (csvUpload.IncludeInvoice) csv.Configuration.RegisterClassMap<PacketUploadInvoiceMasterMap>();
                                else csv.Configuration.RegisterClassMap<PacketUploadBasicMasterMap>();

                                IEnumerable<PacketUploadMaster> records = csv.GetRecords<PacketUploadMaster>().ToList();

                                pumResults = records.ToList();
                            }
                        }
                    }
                }

                response.Close();
            }
Isbell answered 11/4, 2016 at 23:43 Comment(5)
you shouldn't call close and use it as a disposable.Excellence
that does not resolve the issue thoughIsbell
could you elaborate, what library (CsvReader) do you use?Leduc
CsvHelper joshclose.github.io/CsvHelperIsbell
There is a known issue when Read returns 0 bytes it will close the stream developercommunity.visualstudio.com/t/…Paddlefish
I
4

In order to fix this issue, I had to do File reading and writing in csv separately like below

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream())
{
    if (responseStream != null)
    {
        using (MemoryStream ms = new MemoryStream())
        {
            responseStream.CopyTo(ms);
            streamInByte = ms.ToArray();
        }
    }
}

using (TextReader tr = new StreamReader(new MemoryStream(streamInByte), Encoding.Default))
{
    using (CsvReader csv = new CsvReader(tr))
    {
        // The value used to escape fields that contain a delimiter, quote, or line ending.
        // csv.Configuration.Quote = '"';
        if (csvUpload.IncludeInvoice) csv.Configuration.RegisterClassMap<PacketUploadInvoiceMasterMap>();
        else csv.Configuration.RegisterClassMap<PacketUploadBasicMasterMap>();

        // Remove white space from header
        csv.Configuration.TrimHeaders = true;

        IEnumerable<PacketUploadMaster> records = csv.GetRecords<PacketUploadMaster>().ToList();

        pumResults = records.ToList();
    }
}
Isbell answered 5/5, 2016 at 13:28 Comment(0)
S
1

When the using of responseStream ends, it will close and dispose both the responseStream and its underlying stream which happens to be your FtpWebResponse response. Which is why calling response.Close() will throw Cannot access a disposed object.

Swindle answered 6/3, 2017 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.