Timed out trying to connect! to an Azure FTP server
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.
I wonder if it is something wrong with my code below, or issue with the nuget package?
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. But sometimes, it connects even after the error.
Package: 27.1.0.
Please find exception after the code.
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"}]
We had the same problem and we found out that Azure behaves very strange when dealing with ftp requests. FTP uses 1 request to establish the connection and after that a second connection for data transfer. Azure sometimes changes the IP-address during this 2 requests: The connection is established with IP-address A and for the data transfer Azure uses IP-address B and this isn't allowed on many firewalls. We used a workaround that we always try to establish the connection up to 5 times.
Had the same issue when trying to download a folder from an Azure WebApp FTP.
Managed to solve it using the following settings:
FtpClient client = new FtpClient(host, userName, password);
client.SslProtocols = SslProtocols.Tls;
client.ValidateAnyCertificate = true;
client.DataConnectionType = FtpDataConnectionType.PASV;
client.DownloadDataType = FtpDataType.Binary;
client.RetryAttempts = 5;
client.SocketPollInterval = 1000;
client.ConnectTimeout = 2000;
client.ReadTimeout = 2000;
client.DataConnectionConnectTimeout = 2000;
client.DataConnectionReadTimeout = 2000;
We had the same problem and we found out that Azure behaves very strange when dealing with ftp requests. FTP uses 1 request to establish the connection and after that a second connection for data transfer. Azure sometimes changes the IP-address during this 2 requests: The connection is established with IP-address A and for the data transfer Azure uses IP-address B and this isn't allowed on many firewalls. We used a workaround that we always try to establish the connection up to 5 times.
any other workaround? I have the same problem ?
Closing because this appears to be an Azure specific issue and it seems like there is nothing we can do about it.
Hello guys. Any success resolving this issue. I am interested in those settings you mentioned. As far as I understand what you are doing with them is:
- Retry Count from the default 3 to 5.
- Apply fail fast mechanism, so you don't wait for 15 seconds before a retry.
I will try to use these settings and I will come back with a response, if they worked.
@Valkozaur
Hi there.
May I add my opinion on this?
Firstly, from the OP:
"message":"Error connecting FTP server:144.87.142.205 port:21
This tells me, we fail to connect to the control connection.
Then the next post by another user:
Azure sometimes changes the IP-address during this 2 requests: The connection is established with IP-address A and for the data transfer Azure uses IP-address B and this isn't allowed on many firewalls.
This would be a connect failure on the data connection.
That is a definite contradiction.
He also says nothing about PASV, EPSV etc. and provides no logs to corroborate the statement. It is true, though, that content aware firewalls are configured differently or have differing amounts of "smartness" and might not be so happy about FTP changing IP addresses, especially since they might know it is FTP but cannot see a PASV with a differing IP address due to encryption.
Using retries, as many as may be needed, might be a solution to the second problem, if actually truly diagnosed.
But you would certainly agree that this problem has not been debugged, diagnosed or extensively checked at all (I hope :-) ),
So, if you are actually experiencing this and would like to find out more, please open a new issue with the appropriate title and provide a log, using a current version of FluentFTP, which provides very much more data about what is really going on.
I would be very interested to see it, then.
We autodetect azure FTP servers - https://github.com/robinrodricks/FluentFTP/wiki/Server-Information#how-is-azure-ftp-detected and try to auto-conf ourselves.
For connecting from within an Azure VM to something else, sorry, not tried it yet.
That would explain the two differing error descriptions