首页 > 解决方案 > 无法从免费 Azure 应用服务连接到远程 SFTP 服务器

问题描述

我有一个在本地运行良好的 Asp.Net Core 网站我能够连接到 SFTP 服务器并获取文件。但是,一旦我部署到 Azure 应用服务,尝试建立连接就会超时。

我得到的例外是

2019-09-03 10:37:28.7942||ERROR|ExclusiveCard.Services.SFTPProvider|Renci.SshNet.Common.SshOperationTimeoutException: Session operation has timed out
   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.Connect()
   at ExclusiveCard.Services.SFTPProvider.TestConnection() in SFTPProvider.cs:line 237 Renci.SshNet.Common.SshOperationTimeoutException: Session operation has timed out
   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle, TimeSpan timeout)
   at Renci.SshNet.Session.WaitOnHandle(WaitHandle waitHandle)
   at Renci.SshNet.Session.Connect()
   at Renci.SshNet.BaseClient.Connect()

我使用的代码是

SftpClient sftpClient = null;
    try
    {
        sftpClient = new SftpClient("servaer", 2222, "ExclusiveDev", "password");
}
    catch (Renci.SshNet.Common.SshOperationTimeoutException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
    }

我使用了 Package Renci.SshNet.Async version="1.4.0。此代码在本地运行良好,但在部署时在 Azure App Service 上失败。

非常感谢任何帮助。谢谢。

标签: c#azure-web-app-service

解决方案


听起来您已将 ASP.NET Core 应用程序部署到 Windows 上的 Azure 应用程序服务,并且它试图从 Windows 上的 Azure 应用程序服务连接远程 SFTP 服务器。

所以我确信它永远不会像你预期的那样在 Windows 系统的 Azure App Service 上正常工作,因为Use of Raw SocketsAzure Web App 沙箱有一个网络限制来限制套接字连接的建立,如下所示。

在此处输入图像描述

所以我建议您可以尝试重新构建您的应用程序以将其部署到Linux 上的 Azure 应用服务


推荐阅读