我有一个Azure Functions应用,该应用使用下面的代码连接到FTP服务器。但是,我在连接服务器时出错。
仅当应用程序托管在Azure Functions上并且在我的本地计算机上本地工作时,才会发生错误。
该错误是randam并且是间歇性的,一旦发生,该错误就会继续。但是有时,即使发生错误,它也会连接。
例如,我设置了一个测试,该测试每分钟运行一次,持续一个小时,从头开始运行一段时间,然后未建立连接,此后所有后续请求均失败。
FluentFTP软件包:27.1.0。
使用App Service Plan的天蓝色功能应用程序
请在下面的代码后找到异常。
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;
}
错误消息是
尝试连接超时!
或
连接尝试失败,因为被连接方未 一段时间后正确响应或建立连接 由于连接的主机未能响应而失败
下面是堆栈strace:
[{“ severityLevel”:“错误”,“ parsedStack”:[{“程序集”:“ MyApp.FtpClient,版本= 1.0.0.0,区域性=中性,PublicKeyToken =空”,“方法”:“ MyApp。 FtpClient.FtpClientWrapper + d__3.MoveNext“,”级别“:0,”行“:50,”文件名“:” C:\ _ Repos \ MyApp \ src \ MyApp.FtpClient \ FtpClientWrapper.cs“},{” assembly“: “ System.Private.CoreLib,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 7cec85d7bea7798e”,“方法”:“ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw”,“级别”:1,“行”:0} ,{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 7cec85d7bea7798e”,“方法”:“ System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess”,“级别”:2, “ line”:0},{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e”,“方法”:“ System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification”, “ level”:3,“ line”:0},{“ assembly”:“ MyApp.AppService,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null”,“ method”:“ MyApp.AppServi ce.Services.DeviceService +
d.MoveNext“,”级别“:4,”行“:95,”文件名“:” C:\ _ Repos \ MyApp \ src \ MyApp.AppService \ Services \ DeviceService.cs“} ,{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 7cec85d7bea7798e”,“方法”:“ System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw”,“ level”:5, “ line”:0},{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e”,“方法”:“ System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess”, “ level”:6,“ line”:0},{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,Culture =中性,PublicKeyToken = 7cec85d7bea7798e”,“ method”:“ System.Runtime.CompilerServices .TaskAwaiter.HandleNonSuccessAndDebuggerNotification“,”级别“:7,”行“:0},{” assembly“:” MyApp.AppService,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null“,” method“:” MyApp .AppService.Services.DeviceService + d__8.MoveNext“,”级别“:8,”行“:71,”文件名“:” C:\ _ Repos \ MyApp \ src \ MyApp.AppService \ Services \ DeviceService。 cs“},{” assembly“:” System.Private.CoreLib,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw“,”级别“ :9,“ line”:0},{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e”,“方法”:“ System.Runtime.CompilerServices.TaskAwaiter。 ThrowForNonSuccess“,”级别“:10,”行“:0},{” assembly“:” System.Private.CoreLib,版本= 4.0.0.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e“,”方法“:”系统。 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”,“级别”:12,“行”:47,“文件名”:“ C:\ _ Repos \ MyApp \ src \ MyApp.AppService \ Services \ DeviceService.cs “},{” assembly“:” System.Private.CoreLib,版本= 4.0.0.0,区域性=中性,PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.Ex ceptionServices.ExceptionDispatchInfo.Throw“,” level“:13,” line“:0},{” assembly“:” System.Private.CoreLib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = 7cec85d7bea7798e“,”方法“ ::“ System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess”,“级别”:14,“行”:0},{“ assembly”:“ System.Private.CoreLib,版本= 4.0.0.0,文化=中性,PublicKeyToken = 7cec85d7bea7798e“,”方法“:” System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification“,” level“:15,” line“:0},{” assembly“:” MyApp.AzureFunction,Version = 1.0.0.0,Culture =中性,PublicKeyToken = null“,”方法“:” 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 filePath:/ pub / RMGX012 -2907.test.xlsx userName:E001175,ReadAllBytesAsync“,” type“:” System.Exception“,” id“:” 3 7719126“},{” severityLevel“:”错误“,” parsedStack“:[{”程序集“:” FluentFTP,Version = 27.1.0.0,Culture = neutral,PublicKeyToken = f4af092b1d8df44f“,” method“:” FluentFTP.FtpSocketStream。 Connect“,”级别“:0,”行“: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”,“级别”:2,“行”:0},{“汇编”:“ MyApp.FtpClient,版本= 1.0.0.0,文化=中性,PublicKeyToken =空”,“方法”:“ MyApp.FtpClient.FtpClientWrapper + d__3。 MoveNext“,” level“:3,” line“:32,” fileName“:” C:\ _ Repos \ MyApp \ src \ MyApp.FtpClient \ FtpClientWrapper.cs“}],” outerId“:” 37719126“,”消息“:”尝试连接超时!“,”类型“:” System.TimeoutException“,” id“:” 42533996“}]