13

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 code:

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)
    {                        
        using (var saveFile = File.OpenWrite(localPath + "\\" + file.Name))
        {
            //sftp.DownloadFile(file.FullName,saveFile); <-- This works fine
            tasks.Add(Task.Factory.FromAsync(client.BeginDownloadFile(file.FullName, saveFile), client.EndDownloadFile));
        }                        
    }

    await Task.WhenAll(tasks);
    client.Disconnect();

}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
spersson
  • 538
  • 1
  • 8
  • 19

1 Answers1

16

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);
        }
    }
}
Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758
  • Thank you for the answer, however, I still get the same empty files when I try this. No exception either. – spersson Dec 02 '15 at 10:16
  • 3
    @spersson, I updated my answer. Looks like there is no advantage in using `BeginDownloadFile`, so you might as well use the synchronous version. – Thomas Levesque Dec 02 '15 at 10:47
  • Thanks for taking the time, I guess I'll use the synchronous version then :) – spersson Dec 02 '15 at 11:40
  • 4
    Though it is a bummer that they don't use IO completion ports in their implementation, the advantage to using the `Begin*` async overloads over creating your own thread is that some of their IAsyncResult implementations expose a cancellation mechanism, should you need to halt transfers midway through. I wrapped BeginUploadFile in an extension method to make things feel more modern. Check it out: https://gist.github.com/ronnieoverby/438034b19531e6272f98 – Ronnie Overby Feb 25 '16 at 22:44