2

Windows サービスで ftp に問題があります。ftp 経由でファイルを送信するジョブをスケジュールしました。たまにタイムアウトが発生し (頻度は週に 1 回または月に 1 回)、Windows サービスを再起動するまで続きます。

System.Net.WebException: 操作がタイムアウトしました。

私は例外を処理しており、最後に、開いている ftp セッションをすべて閉じます。

try
        {
            string uri = String.Format("ftp://{0}/{1}/{2}", server, download, file);
            Uri serverUri = new Uri(uri);
            if (serverUri.Scheme != Uri.UriSchemeFtp)
            {
                return;
            }
            FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
            reqFTP.Credentials = new NetworkCredential(username, password);
            reqFTP.KeepAlive = false;
            reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
            reqFTP.EnableSsl = false;
            reqFTP.Proxy = null;
            reqFTP.UsePassive = true;
            reqFTP.Timeout = Settings.Default.TimeOut;
            reqFTP.ReadWriteTimeout = Settings.Default.TimeOut;

            response = (FtpWebResponse)reqFTP.GetResponse();
            responseStream = response.GetResponseStream();

            using (FileStream writeStream = new FileStream(path + file, FileMode.Create))
            {
                int Length = 10240;
                Byte[] buffer = new Byte[Length];
                int bytesRead = responseStream.Read(buffer, 0, Length);
                while (bytesRead > 0)
                {
                    writeStream.Write(buffer, 0, bytesRead);
                    bytesRead = responseStream.Read(buffer, 0, Length);
                }
            }

            response.Close();
        }
        catch (WebException wEx)
        {
            LogDatabase.WriteLog("Download File", wEx.ToString(), "Download File");
        }
        finally
        {
            if (response != null)
            {
                response.Close();
            }
            if (responseStream != null)
            {
                responseStream.Close();
            }
        }

何か案は ?

事前にt​​hnx。

4

1 に答える 1

0

なぜこれをすべてループに入れることができないのですか?次に、エラーが発生した場合、ループは戻って再試行します。

また、なぜKeepAliveオプションをfalseに設定したのですか?

私はこれを少しの間遊んで、それをよりよく見ることができるようにクラスに入れましたが、それについてのテストはしませんでした。私のクラスはバックグラウンドスレッドでFTP呼び出しを行います。これは、これと通信できるようにしたい場合は、確かに実行したいことです。

私は確かにこれが少なくともいくつかのグリッチなしで機能することを保証しません!

class FtpRequests {

  private const int BUF_SIZE = 10240;
  private const string PASSWORD = "password";
  private const string USERNAME = "username";
  private const string SERVER = "yourserver.com";
  private string path;

  public FtpRequests() {
    Cancel = false;
    path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
  }

  public bool Cancel { get; set; }

  public bool Complete { get; set; }

  public Thread Thread1 { get; set; }

  public int Timeout { get; set; }

  public int ReadWriteTimeout { get; set; }

  public void StartFtpDownload(string download, string file) {
    string objString = string.Format("{0};{1}", download, file);
    Thread1 = new Thread(startFtpThread);
    Thread1.Name = string.Format("{0} download", file);
    Thread1.IsBackground = true;
    Thread1.Start(objString);
  }

  private void startFtpThread(object obj) {
    Complete = false;
    string objString = obj.ToString();
    string[] split = objString.Split(';');
    string download = split[0];
    string file = split[1];
    do {
      try {
        string uri = String.Format("ftp://{0}/{1}/{2}", SERVER, download, file);
        Uri serverUri = new Uri(uri);
        if (serverUri.Scheme != Uri.UriSchemeFtp) {
          Cancel = true;
          return;
        }
        FtpWebRequest reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
        reqFTP.Credentials = new NetworkCredential(USERNAME, PASSWORD);
        reqFTP.KeepAlive = true;
        reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
        reqFTP.EnableSsl = false;
        reqFTP.Proxy = null;
        reqFTP.UsePassive = true;
        reqFTP.Timeout = Timeout;
        reqFTP.ReadWriteTimeout = ReadWriteTimeout;
        using (FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse()) {
          using (Stream responseStream = response.GetResponseStream()) {
            using (FileStream writeStream = new FileStream(path + file, FileMode.Create)) {
              Byte[] buffer = new Byte[BUF_SIZE];
              int bytesRead = responseStream.Read(buffer, 0, BUF_SIZE);
              while (0 < bytesRead) {
                writeStream.Write(buffer, 0, bytesRead);
                bytesRead = responseStream.Read(buffer, 0, BUF_SIZE);
              }
            }
            responseStream.Close();
          }
          response.Close();
          Complete = true;
        }
      } catch (WebException wEx) {
        LogDatabase.WriteLog("Download File", wEx.ToString(), "Download File");
      }
    } while (!Cancel && !Complete);
  }

}
于 2012-04-06T15:28:30.830 に答える