客户端未与SSH.Net连接异常

时间:2014-08-27 08:56:17

标签: c# ssh.net

我与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:"客户端未连接"时抛出,即使在收益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

1 个答案:

答案 0 :(得分:2)

我也有同样的问题。请仔细阅读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);
            }
        }