2021年1月5日星期二

.NET/C#程序开发中如何更优美地实现失败任务重试的逻辑?

背景

 在.NET中,异常是指成员没有完成它的名称宣称可以完成的行动。在异常的机制中,异常和某件事情的发生频率无关。有时候需要对一些失败的任务进行多次的重试,如果重试的次数达到我们设定的阀值,则再放弃任务。

解决方案

使用一个静态类和静态的泛型方法来处理,创建通用的任务重试机制,我们可以使用Action作为参数。

实现方法

using BQoolCommon.Service.ExtensionMethod;using Newtonsoft.Json.Linq;using System;using System.Collections.Generic;using System.Configuration;using System.Linq;using System.Net;using System.Net.Http;using System.Net.Http.Headers;using System.Security.Cryptography;using System.Text;using System.Threading.Tasks;namespace BQoolCommon.Service.Common{ public static class CommonTools {  static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();  static int sleepMillisecondsTimeout = 1000;  /// <summary>  /// 若發生 Exception (資料庫查詢逾時),重複執行相同動作  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="handler"></param>  /// <param name="retryTimes">預設重試 3次,傳入 0直接 return default(T)</param>  /// <returns></returns>  public static T Retry<T>(Func<T> handler, int retryTimes = 3)  {   if (retryTimes <= 0)   {    return default(T);   }   try   {    return handler();   }   catch (Exception e)   {    retryTimes--;    logger.Error($"剩餘重試次數: {retryTimes}, retry error: {e.Message}, Exception detail: {e.ToJsonString()}");    System.Threading.Thread.Sleep(sleepMillisecondsTimeout);    return Retry(handler, retryTimes);   }  }  /// <summary>  /// 傳入多個動作,遇到 Exception依序執行 (資料庫查詢逾時,改用不同條件查詢)  /// </summary>  /// <typeparam name="T"></typeparam>  /// <param name="handlers"></param>  /// <returns></returns>  public static T Retry<T>(params Func<T>[] handlers)  {   for (int i = 0; i < handlers.Length; i++)   {    var handler = handlers[i];    try    {     return handler();    }    catch (Exception e)    {     logger.Error($"第 {i}次執行錯誤(start from 0): retry error: {e.Message}, Exception detail: {e.ToJsonString()}");     System.Threading.Thread.Sleep(sleepMillisecondsTimeout);    }   }   return default(T);  }  /// <summary>  /// 若發生 Exception (資料庫查詢逾時),重複執行相同動作  /// </summary>  /// <param name="handler"></param>  /// <param name="retryTimes">預設重試 3次,傳入 0直接 return</param>  public static void Retry(Action handler, int retryTimes = 3)  {   if (retryTimes <= 0)   {    return;   }   try   {    handler();   }   catch (Exception e)   {    retryTimes--;    logger.Error($"剩餘重試次數: {retryTimes}, retry error: {e.Message}, Exception detail: {e.ToJsonString()}");    System.Threading.Thread.Sleep(sleepMillisecondsTimeout);    Retry(handler, retryTimes);   }  } }}

调用方法

调用方法如下:

 public List<SystemMailModel> GetSendMailList(SystemMailSearchModel mailSearchModel)  {   return Common.CommonTools.Retry(() => _spSystemSettingsDapperRep.GetSendMailList(mailSearchModel));  }

当然,你也可以自己重载一个async的异步方法。

 









原文转载:http://www.shaoqun.com/a/505967.html

跨境电商:https://www.ikjzd.com/

汇通达:https://www.ikjzd.com/w/1758

retriever:https://www.ikjzd.com/w/773


背景在.NET中,异常是指成员没有完成它的名称宣称可以完成的行动。在异常的机制中,异常和某件事情的发生频率无关。有时候需要对一些失败的任务进行多次的重试,如果重试的次数达到我们设定的阀值,则再放弃任务。解决方案使用一个静态类和静态的泛型方法来处理,创建通用的任务重试机制,我们可以使用Action作为参数。实现方法usingBQoolCommon.Service.ExtensionMethod;us
easel:easel
amazon go:amazon go
2135亿!十年天猫,2018双11完美收官:2135亿!十年天猫,2018双11完美收官
外贸大神高回复率的开发信,客户不回复都难!:外贸大神高回复率的开发信,客户不回复都难!
华住为焦裕禄干部学院扩建宿舍 支持兰考公益慈善事业发展:华住为焦裕禄干部学院扩建宿舍 支持兰考公益慈善事业发展

没有评论:

发表评论

注意:只有此博客的成员才能发布评论。