C#Windows服务在发布模式下在FtpWebRequest.GetResponse()处超时

时间:2016-11-03 01:55:37

标签: c# .net ftp timeout

我创建了一个简单的Windows服务,每隔20秒检查一个FTP文件夹,并将所有文件本地复制到计算机上。

在调试模式下它可以正常工作,但是当我使用installutil安装发行版时,我在调用时发现以下错误

 using (response = reqFTP.GetResponse())
  

FTP错误... System.Net.WebException:操作已超时   System.Net.FtpWebRequest.SyncRequestCallback(Object obj)at   System.Net.FtpWebRequest.RequestCallback(Object obj)at   System.Net.CommandStream.Dispose(布尔处理)at   System.IO.Stream.Dispose()at的System.IO.Stream.Close()   System.Net.ConnectionPool.Destroy(PooledStream pooledStream)at   System.Net.ConnectionPool.PutConnection(PooledStream pooledStream,   Object owningObject,Int32 creationTimeout,Boolean canReuse)at   System.Net.FtpWebRequest.FinishRequestStage(RequestStage阶段)at   System.Net.FtpWebRequest.GetResponse()at   FileMan.Service1.GetFileList()in ....

完整请求如下;

 string[] downloadFiles;
            StringBuilder result = new StringBuilder();
            WebResponse response = null;
            StreamReader reader = null;
            textWrite.WriteLine("attempting to get file list" + DateTime.Now + Environment.NewLine, true);
            try
            {
                FtpWebRequest reqFTP;
                ServicePointManager.DefaultConnectionLimit = 100;
                reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(downloadSrc));
                reqFTP.UseBinary = true;
                textWrite.WriteLine("requestFTP...Passed..." + DateTime.Now + Environment.NewLine, true);
                //reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
                reqFTP.Proxy = null;
                reqFTP.KeepAlive = true;
                reqFTP.UsePassive = false;
                reqFTP.Timeout = 8000;

                //////////STOPS HERE
                //response = reqFTP.GetResponse();
                using (response = reqFTP.GetResponse())
                {
                    // Do stuff
                    //...

                    reader = new StreamReader(response.GetResponseStream());
                    string line = reader.ReadLine();

                    while (line != null)
                    {
                        result.Append(line);
                        result.Append("\n");
                        line = reader.ReadLine();
                    }
                    result.Remove(result.ToString().LastIndexOf('\n'), 1);
                    return result.ToString().Split('\n');
                }
            }
            catch (Exception ex)
            {
                textWrite.WriteLine("FTP error..." + ex + "..." + DateTime.Now + Environment.NewLine, true);
                textWrite.Close();
                if (reader != null)
                {
                    reader.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                downloadFiles = null;
                return downloadFiles;
            }

我需要添加任何权限吗? 由于这在调试模式下工作,代码本身很好。我只需要找出阻止它的内容

P.S。 我使用LocalSystem作为帐户,并在使用installutil安装之前将发布版本直接复制到c驱动器。

1 个答案:

答案 0 :(得分:0)

通过关闭公共防火墙,该程序工作正常! 我刚刚将服务exe添加到了inbounds防火墙规则并且它工作了希望这可以帮助其他有类似麻烦的人