SSH.Net Async file download

前端 未结 1 1257
一向
一向 2021-02-04 13:18

I am trying to download files asynchronously from an SFTP-server using SSH.NET. If I do it synchronously, it works fine but when I do it async, I get empty files. This is my cod

相关标签:
1条回答
  • 2021-02-04 13:59

    Because saveFile is declared in a using block, it is closed right after you start the task, so the download can't complete. Actually, I'm surprised you're not getting an exception.

    You could extract the code to download to a separate method like this:

    var port = 22;
    string host = "localhost";
    string username = "user";
    string password = "password";
    string localPath = @"C:\temp";
    
    using (var client = new SftpClient(host, port, username, password))
    {
        client.Connect();
        var files = client.ListDirectory("");
    
        var tasks = new List<Task>();
    
        foreach (var file in files)
        {                        
            tasks.Add(DownloadFileAsync(file.FullName, localPath + "\\" + file.Name));
        }
    
        await Task.WhenAll(tasks);
        client.Disconnect();
    
    }
    
    ...
    
    async Task DownloadFileAsync(string source, string destination)
    {
        using (var saveFile = File.OpenWrite(destination))
        {
            var task = Task.Factory.FromAsync(client.BeginDownloadFile(source, saveFile), client.EndDownloadFile);
            await task;
        }
    }
    

    This way, the file isn't closed before you finish downloading the file.


    Looking at the SSH.NET source code, it looks like the async version of DownloadFile isn't using "real" async IO (using IO completion port), but instead just executes the download in a new thread. So there's no real advantage in using BeginDownloadFile/EndDownloadFile; you might as well use DownloadFile in a thread that you create yourself:

    Task DownloadFileAsync(string source, string destination)
    {
        return Task.Run(() =>
        {
            using (var saveFile = File.OpenWrite(destination))
            {
                client.DownloadFile(source, saveFile);
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题