SFTP Upload using sshnet library - Handling Connection Issues

TommyGunn32 picture TommyGunn32 · Sep 9, 2014 · Viewed 10.2k times · Source

Using Renci's sshnet library, I am unable to handle connection issues while uploading a file.

If I set a break point on UploadFile, disable my connection, and let it run, it just hangs on that line. These are potentially big files on slow connections, so adding an OperationTimeout would be difficult.

Also, this works fine over and over on consistent connections. Any ideas? Thanks!

AuthenticationMethod[] methods = new AuthenticationMethod[1];
methods[0] = new PasswordAuthenticationMethod(username, pwd);

var con = new ConnectionInfo(Config.RemoteHostName, username, methods);
con.Timeout = new TimeSpan(0, 0, 0, 30);
var client = new SftpClient(con);

client.Connect();

string fullFilePath = string.Format("{0}{1}", Config.RemoteFilePath, filename);
client.BufferSize = 15000;
client.UploadFile(new MemoryStream(buffer), fullFilePath);
client.Disconnect();

Answer

Martin Prikryl picture Martin Prikryl · Sep 9, 2014

When transferring files, The SftpClient.OperationTimeout determines how long the library waits for a single buffer read/write operation (64 KB max by default, see SftpClient.BufferSize). Not how long it waits for whole transfer to complete.

So you can safely change it from the default "infinite" to some meaningful value.