System.Net.FtpClient openwrite犯规上传文件,除非我插入exitting前睡眠 [英] System.Net.FtpClient openwrite doesnt upload file unless I insert a sleep before exitting

查看:775
本文介绍了System.Net.FtpClient openwrite犯规上传文件,除非我插入exitting前睡眠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是System.Net.FtpClient组件上传文件到测试FTP站点。当我运行下面的代码的文件没有出现在远程位置,除非我用了Thread.Sleep按如下(我宁愿不使用):

I'm using the System.Net.FtpClient assembly to upload a file to a test FTP site. When I run the below code the file doesn't appear in the remote location unless I use a Thread.Sleep as per below (which I'd prefer not to use):

using System;
using System.IO;
using System.Net;
using System.Net.FtpClient;
using System.Security.Cryptography.X509Certificates;
using System.Threading;

namespace FtpsUploadTest
{
    /// <summary>
    /// The ftp publisher.
    /// </summary>
    public class FtpPublisher
    {
        private readonly FtpsSettings _settings;
        private readonly IFtpClient _ftpClient;

        /// <summary>
        /// Initializes a new instance of the <see cref="FtpPublisher"/> class.
        /// </summary>
        public FtpPublisher()
        {
            _ftpClient = new FtpClient();
            _settings = SettingsReader.GetMySettings();
            Init();
        }


        /// <summary>
        /// The publish.
        /// </summary>
        /// <param name="fileToUpload">
        /// The input file path.
        /// </param>
        public void Publish(string fileToUpload)
        {
            var remoteFileName = Path.GetFileName(fileToUpload);

            Console.WriteLine("FTPS host: {0} remote path: {1}", _settings.FtpsRemoteHost, _settings.FtpsRemotePath);

            if (!_ftpClient.IsConnected)
            {
                _ftpClient.Connect();
            }

            var fullRemotePath = string.Format("{0}/{1}", _settings.FtpsRemotePath, remoteFileName);

            using (var ftpStream = _ftpClient.OpenWrite(fullRemotePath))
            using (var inputStream = new FileStream(fileToUpload, FileMode.Open))
            {
                inputStream.CopyTo(ftpStream);
                Thread.Sleep(5000);  // <------------------- DOESNT WORK IF REMOVE THIS SLEEP!!
            }

            Console.WriteLine("File '{0}' published successfully", fileToUpload);
        }


        private void Init()
        {
            _ftpClient.Host = _settings.FtpsRemoteHost;
            _ftpClient.Port = _settings.FtpsRemotePort;
            _ftpClient.DataConnectionConnectTimeout = 60000;
            _ftpClient.ConnectTimeout = 60000;
            _ftpClient.Credentials = new NetworkCredential(_settings.FtpsUserId, string.Empty);
            _ftpClient.DataConnectionType = 0;

            if (string.IsNullOrEmpty(_settings.CertFile) || string.IsNullOrEmpty(_settings.CertPassword))
            {
                return;
            }

            _ftpClient.ClientCertificates.Add(CreateCertificate(_settings.CertFile, _settings.CertPassword));
            _ftpClient.EncryptionMode = (FtpEncryptionMode)2;
            _ftpClient.DataConnectionEncryption = true;
        }


        private X509Certificate CreateCertificate(string certFile, string certPassword)
        {
            return new X509Certificate(certFile, certPassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet);
        }
    }
}



任何人都知道我如何能得到它不使用Thread.sleep代码工作?我试过潮红,关闭流,但这样做没有帮助。

Anyone know how I can get it to work without using a Thread.Sleep? I've tried flushing, closing streams but that doesn't help.

推荐答案

远程FTP服务器完全异步地从你的行动码。根据它使该文件可用之前它可能做的事情一样扫描病毒或其他簿记服务器的配置。可能有什么可以做这个,除非你有超过FTP服务器直接控制。即使如此,它可能需要深入配置更改一些漂亮的,甚至是不同的软件包。

The remote FTP server is acting completely asynchronously from your code. Depending on the configuration of the server it might do things like scan for viruses or other bookkeeping before it makes the file available. There may be nothing you can do about this unless you have direct control over the FTP server. Even then it might require some pretty in depth configuration changes, or even a different software package.

这可能会为你工作的一件事是要经过民意调查的文件在完成上载。做一个循环来检查文件,等待1秒,然后重复,直到找到文件或放弃。在异步/等待模式,或者从不同的线程回叫,可以帮助你摆脱任何UI从这个冻结,如果这是一个问题。

One thing that might work for you is to "poll" for the file after completing the upload. Make a loop that checks for the file, waits 1 second, then repeats until it finds the file or gives up. The Async / Await pattern, or a call-back from a different thread, can help you get rid of any UI freezes from this if it's a problem.

这篇关于System.Net.FtpClient openwrite犯规上传文件,除非我插入exitting前睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆