简体   繁体   English

具有重试计数和重试延迟的文件上传-C#中的最佳方法

[英]File Upload with Retry Count and Retry Delay -Best Approach in c#

I have a case there i want upload files with Retry count and Retry delay 我有一种情况,我要上传具有重试计数和重试延迟的文件

Currently i am doing upload like 目前我正在上传像

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;
            }
        }

    }

How can i modify it to include logic for Retry count and retry delay. 我如何修改它以包括重试计数和重试延迟的逻辑。 Can anyone help on this. 任何人都可以帮忙。

I would refactor your code this way: 我将以这种方式重构您的代码:

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;
        }
    }
}

For your delay you could use thread.sleep but or use some kind of timer. 对于您的延迟,您可以使用thread.sleep但可以使用某种计时器。 I hope this helps you on your way. 希望这对您有所帮助。 EDIT: I like a mix of my answer with Donald Jansen's answer! 编辑:我喜欢唐纳德·詹森(Donald Jansen)的答案!

You could use recurrsion and to delay the next try use Task.Delay(..).Wait() Something like the following 您可以使用递归,并延迟下一次尝试,请使用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);
}

Edit: I would mix sanders answer with mine, I didn't include the try..catch..finaly in mine which he did 编辑:我会把沙磨机的答案与我的答案混在一起,我没有将try..catch..finaly包括在我所做的尝试中

Edit2: If you take the UploadFile from sander and use the following it should work great 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);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM