1

I am having an odd issue much like the one described here. Consistent FTP timeout in a scheduled windows service . It's unfortunate there seems to be no solution on this one because I have similar problems. I start the service this is contained in and it works fine for hours,days or weeks sometimes then all of a sudden errors then will timeout until I restart the service.

The first error it seems to return is this rather unhelpful error.

System.Net.WebException: The underlying connection was closed: An unexpected error occurred on a receive. at System.Net.FtpWebRequest.GetResponse()

Then on it's next run it will return this error until I restart the service.

System.Net.WebException: The operation has timed out. at System.Net.FtpWebRequest.GetResponse()

I don't really know what the first error means but I assume it's connected to the timeout issues I am having after that error occurs. I believe I am closing and using things correctly that everything should be cleaned up after I'm done making the ftp requests.

            var format = string.Format("{0}/{1}/{2}/{3}", date.ToString("yyyy"), date.ToString("MM"), date.ToString("dd"), date.AddHours(1).ToString("HH"));
            var request = (FtpWebRequest)FtpWebRequest.Create("ftp://blahblah/" + format);
            request.Method = WebRequestMethods.Ftp.ListDirectory;
            request.KeepAlive = false;
            request.UsePassive = true;
            request.Timeout = 180000;//3 mins
            try
            {
                using (var response = request.GetResponse())
                {
                    using (var responseStream = response.GetResponseStream())
                    {
                        if (responseStream != null)
                        {
                            using (var reader = new StreamReader(responseStream))
                            {
                                file = reader.ReadToEnd();
                                var files = file.Split('\n');
                                foreach (var s in files.Where(s => !String.IsNullOrEmpty(s)))
                                {
                                    file = s.Replace("\r", "");
                                }
                                reader.Close();
                            }
                            responseStream.Close();
                        }
                    }
                    response.Close();
                }
            }

Thanks for any help.

Community
  • 1
  • 1
Kohins
  • 327
  • 6
  • 21
  • Have you turned on [network tracing](http://stackoverflow.com/a/9666598/2101267) yet? – Dark Falcon Dec 08 '14 at 17:46
  • Is there a way to limit file size? Considering I'm using this connection fairly often, several times per minute in some cases, I think the file would grow excessively quite quickly. – Kohins Dec 08 '14 at 17:55
  • You could always [write your own implementation](http://msdn.microsoft.com/en-us/library/4y5y10s7.aspx) of TraceListener which limits the total size of the trace. Unfortunately I'm not aware of a built-in provider which does this. – Dark Falcon Dec 09 '14 at 13:08

0 Answers0