我在使用 Renci SSH.Net 时遇到了一个奇怪的问题:
var sftp = new SftpClient(remoteHost, remotePort, remoteUserName, remotePassword);
try
{
sftp.Connect();
using (var file = new FileOutputStream(filePath))
{
sftp.DownloadFile(remoteFileName, file);
}
sftp.Disconnect(); // *
}
catch (Exception ex)
{
// log stuff
throw;
}
finally
{
sftp.Dispose();
}
上面的代码在 //*
处抛出 SshConnectionException
:“Client not connected”,即使在检查 sftp.IsConnected
时也是如此在产生 true
之前。
文件按预期下载。
堆栈跟踪如下:
at Renci.SshNet.Session.SendMessage(Message message)
at Renci.SshNet.Session.SendDisconnect(DisconnectReason reasonCode, String message)
at Renci.SshNet.Session.Disconnect()
at Renci.SshNet.BaseClient.Disconnect()
at My.Program.MyMethod() in c:\path\to\my\program.cs:line 42
最佳答案
我也有同样的问题。请通过这个https://sshnet.codeplex.com/workitem/1561找出原因。这是我目前的解决方法:
catch (Exception ex)
{
if (ex is SshConnectionException || ex is SocketException)
{
_ifwInstance.Error(string.Format("Ignoring {0} during listing directory", ex.Message));
}
else
{
Debugger.Log(0, null, ex.InnerException.ToString());
throw new Exception("Login to SFT FAILED", ex);
}
}
关于c# - 客户端未连接异常与 SSH.Net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25522894/