3

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在 yield 之前检查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
4

1 回答 1

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);
            }
        }
于 2014-10-16T20:53:24.217 回答