简体   繁体   中英

FluentFTP connection broke but no exception

I use FluentFTP in my code to transfer data internally to a FTP server. If the connection to the FTP server breaks down during the upload, then there is no exception.

But oddly enough, that doesn't happen with all dates. If I take a *,7z file. there is an exception when the connection is broken.

I'm confused!

When transferring a *.7z file, why does it recognize that the connection was interrupted (service stopped) and restart the connection when the service is available again and with a *.opus file does the program stop in an await?

public class FileWatcher
{
   public static async Task Main(string[] args)
   {
      do
      {
        Console.WriteLine("Und los geht es!");
        await UploadFileAsync();
        await Task.Delay(15000);
      } while (true);
}

static async Task UploadFileAsync()
{
    try
    {
        string[] filePath = Directory.GetFiles(@"C:\temp\ftpupload", "*", 
        SearchOption.AllDirectories);
        var token = new CancellationToken();
        using (AsyncFtpClient client = new AsyncFtpClient())
        {
            client.Host = "192.168.1.100";
            client.Port = 21;
            client.Credentials.UserName = "test";
            client.Credentials.Password = "test123";
            client.Config.EncryptionMode = FtpEncryptionMode.None;
            client.Config.InternetProtocolVersions = FtpIpVersion.IPv4;
            client.Config.ValidateAnyCertificate = true;
            client.Config.ConnectTimeout = 10000;

            Console.WriteLine("Connecting......");

            await client.AutoConnect(token);

            Console.WriteLine("Connected!");


            foreach (var erg in filePath)
            {
                Console.WriteLine("File is uploading: " + erg.GetFtpFileName());
                await client.UploadFile(erg, "/" + erg.GetFtpFileName(), 
                FtpRemoteExists.Overwrite, true, token: token);
                Console.WriteLine("File successfully uploaded: " + 
                erg.GetFtpFileName());
                System.IO.File.Delete(erg);
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
  }
}

Error while uploading the file to the server. See InnerException for more info.

I think the problem is that you are not catching the exception from the Main method. The code inside the try-catch block will execute correctly, but if an exception occurs outside the try-catch block, the program will terminate without reporting the error.

So to fix it, you should add a try-catch block in the Main method and inside it, call the UploadFileAsync() method with the await keyword.

Another reason may be the size of the file, or the delay you set in the Main method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM