C# 具有重试次数和重试延迟的文件上载-c语言中的最佳方法#
我有一个案例,我想上传文件重试计数和重试延迟 目前我正在做上传喜欢C# 具有重试次数和重试延迟的文件上载-c语言中的最佳方法#,c#,.net,file-upload,ssh,C#,.net,File Upload,Ssh,我有一个案例,我想上传文件重试计数和重试延迟 目前我正在做上传喜欢 Status UploadWithRetry(string sourceFile, string destinationFolder, int retryCount, int retryIntervalSeconds) { Status uploadStatus = new FileTransferStatus { FilePath = sourceFile }; var fileNa
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;
}
}
}
如何修改它以包含重试计数和重试延迟的逻辑。有人能帮忙吗。您可以使用Recursion并延迟下一个try-use任务。延迟(..)。等待() 类似于下面的内容
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);
}
编辑:
我会把桑德斯的答案和我的答案混在一起,我没有在我的答案中加入他所做的尝试
编辑2:
如果您从sander获取上传文件并使用以下命令,那么它应该会非常有用
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);
}
您可以使用Recursion并延迟下一个try use 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);
}
编辑:
我会把桑德斯的答案和我的答案混在一起,我没有在我的答案中加入他所做的尝试
编辑2:
如果您从sander获取上传文件并使用以下命令,那么它应该会非常有用
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);
}
我将以这种方式重构您的代码:
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;
}
}
}
状态UploadWithRetry(字符串源文件、字符串目标文件夹、int-maxRetryCount、int-retryIntervalSeconds)
{
var fileName=Path.GetFileName(sourceFile);
var uploadAttempts=0;
var成功=false;
var status=newfiletransferstatus{FilePath=sourceFile};
while(上传尝试=maxRetryCount){
//抛出新异常();//我建议不要这样做
status.Message=“最大重试次数。”;//将此消息显示到前端
}
返回状态;
}
状态上载文件(字符串源文件){
使用(var fileStream=newfilestream(sourceFile,FileMode.Open))
{
状态uploadStatus=新文件传输状态{FilePath=sourceFile};
试一试{
_UploadFile(fileStream,destinationFilePath,null);
状态。成功=正确;
}
捕获(例外情况除外){
状态。成功=错误;
}
返回上传状态;
}
}
}
对于延迟,您可以使用thread.sleep,但也可以使用某种计时器。我希望这对你有所帮助。
编辑:我喜欢把我的答案和唐纳德·詹森的答案混合起来 我会这样重构您的代码:
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;
}
}
}
状态UploadWithRetry(字符串源文件、字符串目标文件夹、int-maxRetryCount、int-retryIntervalSeconds)
{
var fileName=Path.GetFileName(sourceFile);
var uploadAttempts=0;
var成功=false;
var status=newfiletransferstatus{FilePath=sourceFile};
while(上传尝试=maxRetryCount){
//抛出新异常();//我建议不要这样做
status.Message=“最大重试次数。”;//将此消息显示到前端
}
返回状态;
}
状态上载文件(字符串源文件){
使用(var fileStream=newfilestream(sourceFile,FileMode.Open))
{
状态uploadStatus=新文件传输状态{FilePath=sourceFile};
试一试{
_UploadFile(fileStream,destinationFilePath,null);
状态。成功=正确;
}
捕获(例外情况除外){
状态。成功=错误;
}
返回上传状态;
}
}
}
对于延迟,您可以使用thread.sleep,但也可以使用某种计时器。我希望这对你有所帮助。
编辑:我喜欢把我的答案和唐纳德·詹森的答案混合起来 你能解释一下你在使用什么样的框架和技术,以及你到底想做什么吗?您希望为后端提供最大重试次数,还是希望前端计算重试次数?我使用的是ssh客户端。但我需要的是在_client.UploadFile(fileStream,destinationFilePath,null)中应用重试和延迟。。如果发生任何异常(即._client.UploadFile),它应该根据延迟参数重试。您能解释一下您正在使用的框架和技术以及您到底想做什么吗?您希望为后端提供最大重试次数,还是希望前端计算重试次数?我使用的是ssh客户端。但我需要的是在_client.UploadFile(fileStream,destinationFilePath,null)中应用重试和延迟。。如果发生任何异常(即._client.UploadFile),它应该根据延迟参数thx重试,只是用混合答案的建议更新了我的答案。我喜欢你的递归解决方案。。。很多哈哈,我也这么做了。我喜欢上传文件返回一个状态,可以在我的recursive@Sander。我还想引发上次重试尝试中发生的异常。我编辑了答案。在这种情况下,您需要向状态类添加一条字符串消息,您可以使用该消息与前端通信。如果我们需要在特定的时间间隔(即retryIntervalSeconds之后)调用此重试函数。需要做哪些更改?Thx,刚刚更新了我的答案,建议混合我们的答案。我喜欢你的递归解决方案。。。很多哈哈,我也这么做了。我喜欢上传文件返回一个状态,可以在我的recursive@Sander。我还想引发上次重试尝试中发生的异常。我编辑了答案。在这种情况下,您需要向状态类添加一条字符串消息,您可以使用该消息与前端通信。如果我们需要在特定的时间间隔(即retryIntervalSeconds之后)调用此重试函数。需要进行哪些更改?@Sander。另外,我想引发上次重试尝试中发生的异常。您可以做的是在main方法中进行检查,但我不会引发异常。如果您想引发异常,请编辑我的答案。请执行此操作,或者按照Sander的建议添加属性消息,你真的不应该抛出例外