在c#中使用重试计数和重试延迟 - 最佳方法进行文件上载

时间:2015-10-23 09:22:03

标签: c# .net file-upload ssh

我有一个案例,我希望上传文件有重试次数和重试延迟

目前我正在上传像

Status UploadWithRetry(string sourceFile, string destinationFolder, int retryCount, int retryIntervalSeconds)
    {

        Status uploadStatus = new FileTransferStatus { FilePath = sourceFile };

        var fileName = Path.GetFileName(sourceFile);

        using (var fileStream = new FileStream(sourceFile, FileMode.Open))
        {

                _client.UploadFile(fileStream, destinationFilePath, null);
                 Status.Success = true;
                return uploadStatus;
            }
        }

    }

如何修改它以包含重试计数和重试延迟的逻辑。任何人都可以帮忙解决这个问题。

2 个答案:

答案 0 :(得分:3)

我会以这种方式重构您的代码:

Status UploadWithRetry(string sourceFile, string destinationFolder, int maxRetryCount, int retryIntervalSeconds)
{      

    var fileName = Path.GetFileName(sourceFile);
    var uploadAttempts = 0;
    var success = false;
    var status = new FileTransferStatus { FilePath = sourceFile };
    while(uploadAttempts < maxRetryCount && !success){
        status = UploadFile(sourceFile);
        uploadAttempts++;
        success = status.Success;
    }
    if(uploadAttempts >= maxRetryCount){
        //throw new Exception(); //I would advise against this
        status.Message = "Max retry attempts reached."; //display this message to the frontend
    }
    return status;
}

Status UploadFile(string sourceFile){
    using (var fileStream = new FileStream(sourceFile, FileMode.Open))
    {
            Status uploadStatus = new FileTransferStatus { FilePath = sourceFile };
            try{

                _client.UploadFile(fileStream, destinationFilePath, null);
                Status.Success = true;
            }
            catch(Exception ex){
                Status.Success = false;
            }
            return uploadStatus;
        }
    }
}

对于你的延迟你可以使用thread.sleep但是或者使用某种计时器。我希望这能帮到你。 编辑:我喜欢把我的答案和Donald Jansen的答案结合起来!

答案 1 :(得分:2)

您可以使用recurrsion并延迟下一次尝试使用Task.Delay(..)。Wait() 类似于以下内容

Status UploadWithRetry(string sourceFile, string destinationFolder, int retryCount, int retryIntervalSeconds)
{

    Status uploadStatus = new FileTransferStatus { FilePath = sourceFile };

    var fileName = Path.GetFileName(sourceFile);

    var status = default(Status);
    using (var fileStream = new FileStream(sourceFile, FileMode.Open))
    {

            _client.UploadFile(fileStream, destinationFilePath, null);
             Status.Success = true;

            status = uploadStatus;
        }
    }
    if (retryCount == 0)
        return status;
    Task.Delay(TimeSpan.FromSeconds(retryIntervalSeconds)).Wait();
    return UploadWithRetry(sourceFile, destinationFolder, retryCount - 1, retryIntervalSeconds);
}

编辑: 我会把桑德斯的答案和我的混在一起,我没有将try..catch..finaly包含在我的作品中

EDIT2: 如果你从sander上下载UploadFile并使用以下内容它应该很好用

    Status UploadWithRetry(string sourceFile, string destinationFolder, int retryCount, int retryIntervalSeconds)
    {
        var status = UploadFile(sourceFile);

        if (status.Success)
            return status;

        if (retryCount == 0) //OR THROW EXCEPTION HERE
            return status;
        Task.Delay(TimeSpan.FromSeconds(retryIntervalSeconds)).Wait();
        return UploadWithRetry(sourceFile, destinationFolder, retryCount - 1, retryIntervalSeconds);
    }