我正在使用Windows服务,该服务使用SSH.NET将文件上传到sftp服务器。问题是当我运行负责从Windows控制台应用程序发送文件的代码时,一切正常,但是当从Windows服务运行完全相同的代码时,我在连接到SFTP时收到异常消息:“由于目标计算机主动拒绝,所以无法建立连接它”
我在同一台计算机上运行SFTP服务器以进行测试。
public void Send(string host, string userName, int port, string password, string filePath)
{
var connectionInfo = new ConnectionInfo(host, userName, new PasswordAuthenticationMethod(userName, password));
try
{
var sftpClient = new SftpClient(connectionInfo);
sftpClient.Connect(); // exception occurs there
using (FileStream fileStream = File.OpenRead(filePath))
{
sftpClient.UploadFile(fileStream, $"{Path.GetFileName(filePath)}", true);
}
sftpClient.Disconnect();
sftpClient.Dispose();
}
catch (Exception exception)
{
Logger.Log($"{host} | {password} | {port} | {userName}");
Logger.Log(exception.Message);
Logger.Log(exception.StackTrace);
}
}
有人知道发生了什么吗?我认为这可能是一些配置问题,因为就像我说的那样,从控制台应用程序运行相同的代码即可。