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.