C# Renci.SshNet.SftpClient.Connect引发SshConnectionException
我正在C#应用程序中使用Renci.SshNet库(v2016.0.0),该应用程序计划每天向FTP服务器上传一个文件。在我的机器上进行测试时,代码的行为与将文件上载到SFTP服务器的预期一致。但是,部署到生产环境后,我们看到以下异常,即使应用程序正在连接到相同的SFTP主机地址:C# Renci.SshNet.SftpClient.Connect引发SshConnectionException,c#,sftp,ssh.net,C#,Sftp,Ssh.net,我正在C#应用程序中使用Renci.SshNet库(v2016.0.0),该应用程序计划每天向FTP服务器上传一个文件。在我的机器上进行测试时,代码的行为与将文件上载到SFTP服务器的预期一致。但是,部署到生产环境后,我们看到以下异常,即使应用程序正在连接到相同的SFTP主机地址: System.AggregateException: One or more errors occurred. ---> Renci.SshNet.Common.SshConnectionException:
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服务器上的配置问题?首先尝试使用生产机器上的任何SSH/SFTP客户端手动连接。首先尝试使用生产机器上的任何SSH/SFTP客户端手动连接。