我正在使用SSH.NET进行上传。但是如果上传失败,我想重试sftp文件。我有这段代码,但是我不认为这是处理重试的最佳方式。处理这个问题的最佳方式是什么?
var exceptions = new List<Exception>();
int retryCount = 3;
for (int retry = 0; retry < retryCount; retry++)
{
try
{
var filePath = Path.Combine(path, fileName);
var client = new SftpClient("ftp.site.com", 22, "userName", "password");
client.Connect();
if (client.IsConnected)
{
var fileStream = new FileStream(filePath, FileMode.Open);
if (fileStream != null)
{
client.UploadFile(fileStream, "/fileshare/" + fileName, null);
client.Disconnect();
client.Dispose();
}
}
}
catch (Exception ex)
{
exceptions.Add(ex);
System.Threading.Thread.Sleep(10000);
}
if (exceptions.Count == 3)
{
throw exceptions[0];
}
}
1条答案
按热度按时间4c8rllxm1#
推荐的重试方法是瞬时故障处理库Polly。