我有一个 Azure Functions 应用程序,它使用下面的代码连接到 FTP 服务器。但是,我连接到服务器时出错。
仅当应用程序托管在 Azure Functions 上并在我的本地计算机上本地运行时,才会发生该错误。
错误是随机的和间歇性的,一旦发生,错误就会继续。但有时,即使在错误之后它也会连接。
例如,我设置了一个测试,每分钟运行一次,持续一小时,它在开始时工作了一段时间,然后没有建立连接,之后所有后续请求都失败了。
FluentFTP 包:27.1.0。
使用应用服务计划的 Azure 函数应用
请在下面的代码后查找异常。
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;
}
错误信息是
尝试连接超时!
或者
连接尝试失败,因为连接的一方在一段时间后没有正确响应,或者连接失败,因为连接的主机没有响应
下面是堆栈跟踪:
[{"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,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":1,"line":0},{"程序集”:“System.Private.CoreLib,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e”,“方法”:“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" },{"程序集":"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" ,"方法":"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 =中性,PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess","level":10,"line":0},{"assembly":"System.Private.CoreLib, 版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e","方法":"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,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e","method":"System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw","level":13,"line":0},{ “程序集”:“System.Private.CoreLib,版本=4.0.0.0,文化=中性,PublicKeyToken=7cec85d7bea7798e”,“方法”:“System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess”,“级别”: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":"连接 FTP 服务器时出错:144.87.142.205 端口:21 文件路径:/pub/RMGX012-2907.test。xlsx userName:E001175, ReadAllBytesAsync","type":"System.Exception","id":"37719126"},{"severityLevel":"Error","parsedStack":[{"assembly":"FluentFTP, 版本=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":"尝试连接超时!","type":" System.TimeoutException","id":"42533996"}]尝试连接超时!","type":"System.TimeoutException","id":"42533996"}]尝试连接超时!","type":"System.TimeoutException","id":"42533996"}]