1

I have an Azure Functions app that uses the code below to connect to FTP server. However, I got error connecting to the server.

The error only happens when the app is hosted on Azure Functions, and works locally on my local machine.

The error is randam and intermittent, and once it occurred, the error continues. But sometimes, it connects even after the error.

For example, I setup a test that runs once every minute for one hour, it works at the begining for some time, and then the connection is not established, all subsequent requests failed after that.

FluentFTP Package: 27.1.0.

Azure function app using App Service Plan

Please find exception after the code below.

public async Task<byte[]> ReadAllBytesAsync()
     {
         byte[] content = new byte[0];
         try
         {
             using (FluentFTP.FtpClient client = GetFtpClient())
             {

                 client.Connect();

                 if (!client.FileExists(_configSetting.FtpServer.FilePath))
                 {
                     throw new Exception($"{_configSetting.FtpServer.FilePath} doesn't exist");
                 }

                 content = await client.DownloadAsync(_configSetting.FtpServer.FilePath, CancellationToken.None);

                 client.Disconnect();

             }

         }
         catch (Exception e)
         {
             throw new Exception($"Error", e);
         }
         return content;
     }

     public void DeleteFile(string filePath)
     {
         try
         {
             using (FluentFTP.FtpClient client = GetFtpClient())
             {

                 client.Connect();


                 if ( client.FileExists(filePath))
                 {
                     client.DeleteFile(filePath);


                 }

                 client.Disconnect();

             }
         }
         catch (Exception e)
         {
             throw new Exception(
                 $"Error",
                 e);
         }
     }


     private FluentFTP.FtpClient GetFtpClient()
     {

         FluentFTP.FtpClient client = new FluentFTP.FtpClient(_configSetting.FtpServer.Host,
             _configSetting.FtpServer.Port,
             _configSetting.FtpServer.Username,
             _configSetting.FtpServer.Password)
         {
             EncryptionMode = FtpEncryptionMode.Explicit,
             SslProtocols = SslProtocols.Tls 
         };

       client.ValidateCertificate += OnValidateCertificate;


         return client;
     }

      private void OnValidateCertificate(FluentFTP.FtpClient control, FtpSslValidationEventArgs e)
     {
         //accept remote certificate without validation
         e.Accept = true;
     }

The error message is

Timed out trying to connect!

or

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Below is stack strace:

[{"severityLevel":"Error","parsedStack":[{"assembly":"MyApp.FtpClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"MyApp.FtpClient.FtpClientWrapper+d__3.MoveNext","level":0,"line":50,"fileName":"C:\_Repos\MyApp\src\MyApp.FtpClient\FtpClientWrapper.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":1,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":2,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":3,"line":0},{"assembly":"MyApp.AppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"MyApp.AppService.Services.DeviceService+<b__10_0>d.MoveNext","level":4,"line":95,"fileName":"C:\_Repos\MyApp\src\MyApp.AppService\Services\DeviceService.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":5,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":6,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":7,"line":0},{"assembly":"MyApp.AppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"MyApp.AppService.Services.DeviceService+d__8.MoveNext","level":8,"line":71,"fileName":"C:\_Repos\MyApp\src\MyApp.AppService\Services\DeviceService.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":9,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":10,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":11,"line":0},{"assembly":"MyApp.AppService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"MyApp.AppService.Services.DeviceService+d__7.MoveNext","level":12,"line":47,"fileName":"C:\_Repos\MyApp\src\MyApp.AppService\Services\DeviceService.cs"},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":13,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":14,"line":0},{"assembly":"System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification","level":15,"line":0},{"assembly":"MyApp.AzureFunction, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"RMG.PDA.IoTHub.DeviceProvisioningService.FunctionApp.Schedulers.DeviceRegistrationTimerFunction+d__5.MoveNext","level":16,"line":42,"fileName":"C:\_Repos\MyApp\src\MyApp.AzureFunction\Schedulers\DeviceRegistrationTimerFunction.cs"}],"outerId":"0","message":"Error connecting FTP server:144.87.142.205 port:21 filePath:/pub/RMGX012-2907.test.xlsx userName:E001175, ReadAllBytesAsync","type":"System.Exception","id":"37719126"},{"severityLevel":"Error","parsedStack":[{"assembly":"FluentFTP, Version=27.1.0.0, Culture=neutral, PublicKeyToken=f4af092b1d8df44f","method":"FluentFTP.FtpSocketStream.Connect","level":0,"line":0},{"assembly":"FluentFTP, Version=27.1.0.0, Culture=neutral, PublicKeyToken=f4af092b1d8df44f","method":"FluentFTP.FtpClient.Connect","level":1,"line":0},{"assembly":"FluentFTP, Version=27.1.0.0, Culture=neutral, PublicKeyToken=f4af092b1d8df44f","method":"FluentFTP.FtpClient.Connect","level":2,"line":0},{"assembly":"MyApp.FtpClient, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null","method":"MyApp.FtpClient.FtpClientWrapper+d__3.MoveNext","level":3,"line":32,"fileName":"C:\_Repos\MyApp\src\MyApp.FtpClient\FtpClientWrapper.cs"}],"outerId":"37719126","message":"Timed out trying to connect!","type":"System.TimeoutException","id":"42533996"}]

Pingpong
  • 7,681
  • 21
  • 83
  • 209

0 Answers0