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

标签 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:“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/

相关文章:

c# - 随机获取 Renci.SshNet.SftpClient.Connect 抛出 SshConnectionException

c# - Ninject 错误 : Sequence contains no elements

c# - 按组后排序?

c# - 如何在 C# 中的 SSH 服务器上运行命令?

.net - 在 SSH.NET 中更改 key 交换方法

c# - 仅读取命令输出的前几行,而无需等待命令在C#中使用SSH.NET完全完成

c# - 如何在 XML 配置中声明 Unity InjectionFactory

C# 找不到网络路径

c# - System.Random rnd = new System.Random(); 有多随机?

linux - 从 PowerShell 远程执行 Linux 应用程序