Renci.SshNet.SftpClient.Connect抛出SshConnectionException

时间:2017-08-17 16:19:11

标签: c# sftp ssh.net

我正在C#应用程序中使用Renci.SshNet库(v2016.0.0),该应用程序计划每天将文件上传到FTP服务器。在我的机器上进行测试时,代码按预期将文件上载到SFTP服务器。但是,在部署到生产之后,即使应用程序连接到相同的SFTP主机地址,我们也会看到以下异常:

System.AggregateException: One or more errors occurred. ---> Renci.SshNet.Common.SshConnectionException: Server response does not contain SSH protocol identification.
    at Renci.SshNet.Session.Connect()
    at Renci.SshNet.BaseClient.Connect()

抛出异常的相应代码如下:

using (FileStream fileToUpload = new FileStream(exportFilePath, FileMode.Open))
{
    using (var sftpClient = new SftpClient(SftpHost, 22, SftpUserName, new PrivateKeyFile(privateKeyFileName)))
    {
        sftpClient.ErrorOccurred += SftpErrorHandler;

        sftpClient.Connect(); // Line throwing the exception

        if (!sftpClient.IsConnected)
        {
            throw new InvalidOperationException($"An error occurred while connecting to the SFTP server {SftpHost}");
        }

        sftpClient.UploadFile(fileToUpload, fileName);
        sftpClient.Disconnect();
        sftpClient.ErrorOccurred -= SftpErrorHandler;
    }
}

有没有人对如何解决这个问题有任何建议?从我的角度来看,这似乎是一个非常不明显的错误。我想知道这是运行应用程序的机器上的防火墙配置吗?或者它可能是应用程序连接到的SFTP服务器上的配置问题?

0 个答案:

没有答案