【转载】C#异常Retry通用类
//Retry机制
public static class Retry
{
/// <summary>
/// 重试零个参数无返回值的方法
/// </summary>
/// <param name="action">执行方法方法</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute(Action action, TimeSpan retryInterval, int retryCount = 3)
{
Execute<object>(() =>
{
action();
return null;
}, retryInterval, retryCount);
} /// <summary>
/// 重试一个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1>(Action<T1> action, T1 arg1, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, object>((x1) =>
{
action(arg1);
return null;
}, arg1, retryInterval, retryCount);
} /// <summary>
/// 重试两个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1, T2>(Action<T1, T2> action, T1 arg1, T2 arg2, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, T2, object>((x1, x2) =>
{
action(arg1, arg2);
return null;
}, arg1, arg2, retryInterval, retryCount);
} /// <summary>
/// 重试三个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1, T2, T3>(Action<T1, T2, T3> action, T1 arg1, T2 arg2, T3 arg3, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, T2, T3, object>((x1, x2, x3) =>
{
action(arg1, arg2, arg3);
return null;
}, arg1, arg2, arg3, retryInterval, retryCount);
} /// <summary>
/// 重试四个参数无返回值的方法
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <typeparam name="T4">参数类型4</typeparam>
/// <param name="action">执行方法方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="arg4">参数4</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
public static void Execute<T1, T2, T3, T4>(Action<T1, T2, T3, T4> action, T1 arg1, T2 arg2, T3 arg3, T4 arg4, TimeSpan retryInterval, int retryCount = 3)
{
Execute<T1, T2, T3, T4, object>((x1, x2, x3, x4) =>
{
action(arg1, arg2, arg3, arg4);
return null;
}, arg1, arg2, arg3, arg4, retryInterval, retryCount);
} /// <summary>
/// 重试零个参数带返回值
/// </summary>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T>(Func<T> func, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func();
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试一个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T>(Func<T1, T> func, T1 arg1, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试两个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T2, T>(Func<T1, T2, T> func, T1 arg1, T2 arg2, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试三个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T2, T3, T>(Func<T1, T2, T3, T> func, T1 arg1, T2 arg2, T3 arg3, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2, arg3);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
} /// <summary>
/// 重试四个参数带返回值
/// </summary>
/// <typeparam name="T1">参数类型1</typeparam>
/// <typeparam name="T2">参数类型2</typeparam>
/// <typeparam name="T3">参数类型3</typeparam>
/// <typeparam name="T4">参数类型4</typeparam>
/// <typeparam name="T">返回类型</typeparam>
/// <param name="func">执行的方法</param>
/// <param name="arg1">参数1</param>
/// <param name="arg2">参数2</param>
/// <param name="arg3">参数3</param>
/// <param name="arg4">参数4</param>
/// <param name="retryInterval">重试间隔</param>
/// <param name="retryCount">重试次数</param>
/// <returns>返回类型T</returns>
public static T Execute<T1, T2, T3, T4, T>(Func<T1, T2, T3, T4, T> func, T1 arg1, T2 arg2, T3 arg3, T4 arg4, TimeSpan retryInterval, int retryCount = 3)
{
var exceptions = new List<Exception>(); for (int retry = 0; retry < retryCount; retry++)
{
try
{
return func(arg1, arg2, arg3, arg4);
}
catch (Exception ex)
{
exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
} throw new AggregateException(exceptions);
}
}
【转载】C#异常Retry通用类的更多相关文章
- C#异常Retry通用类
系统里面常常调用服务和读写文件里面需要对发生异常,操作失败时进行Retry来尽可能程序的健壮性.最近工作中遇到了,参考了网上的资料,扩展了下.在博客里面备用下: //Retry机制 public st ...
- C#异常重试通用类Retry
//Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...
- Util应用程序框架公共操作类(五):异常公共操作类
任何系统都需要处理错误,本文介绍的异常公共操作类,用于对业务上的错误进行简单支持. 对于刚刚接触.Net的新手,碰到错误的时候,一般喜欢通过返回bool值的方式指示是否执行成功. public boo ...
- 使用C# 操作存储过程,执行sql语句通用类
如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码: /// <summary> /// Sql通用类 /// </summary> ...
- Django子应用里的通用类混合视图处理
# 子应用的views.py此文件用于编写Web应用视图,处理复杂的业务逻辑 #导入方法视图模板 from django.shortcuts import render #导包 from django ...
- C#.NET操作数据库通用类
下面给出了一个C#操作MS SQL Server 数据库的通用类,通过该类可以对数据库进行任何操作,包括执行SQL语句.执行存储过程.以下是其详细实现过程,希望大家共同修改优化之.稍后将介绍如何使用它 ...
- poi导出excel通用类
一.关键的通用类public class PoiExportUtils { private static HSSFWorkbook workBook; public PoiExportUtils ...
- NPOI MVC 模型导出Excel通用类
通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...
- MVC NPOI Linq导出Excel通用类
之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...
随机推荐
- 再谈获取网站图标Icon
上一篇文章讨论了一下获取网站图标方法,是通过从根目录直接获取和html解析结合的方式来获取的,并给出了相应的代码示例.这一篇来讨论一个更现成的方法,这个方法是从360导航的页面发现的,在导航页面中点击 ...
- puthon进程开发
进程 本节目录 一 背景知识 二 什么是进程 三 进程调度 四 并发与并行 五 同步\异步\阻塞\非阻塞 六 进程的创建与结束 七 multiprocess模块 八 进程池和mutiprocess.P ...
- TortoiseSVN的基本使用方法
TotoiseSVN的基本使用方法 来源 https://blog.csdn.net/hecongzhen/article/details/37879801 在 项目管理实践教程一.工欲善其事,必先利 ...
- Zabbix概术及基础介绍(一)
一.Zabbix介绍 Zabbix 是由Alexei Vladishev创建,目前由Zabbix SIA在持续开发和支持.Zabbix 是一个企业级的分布式开源监控方案.Zabbix是一款能够监控各种 ...
- Android APK打包流程
简单build流程图 官网给了我们一张非常简单的编译.打包.apk生成内容以及签名的图片.图片大体介绍了从Project到运行到设备或者模拟器的一个大体流程,我们也从中看到一个完整的apk包含如下内容 ...
- 聊一聊PHP的依赖注入(DI) 和 控制反转(IoC)
简介 IoC Inversion of Control 控制反转DI Dependency Injection 依赖注入 依赖注入和控制反转说的实际上是同一种东西,它们是一种设计模式,这种设计模式用来 ...
- Exchange-fanout 广播模式
一.前言 我们看到生产者将消息投递到Queue中,实际上这在RabbitMQ中这种事情永远都不会发生.实际的情况是,生产者将消息发送到Exchange(交换器,下图中的X),由Exchange将消息路 ...
- 1.5 Scipy:高级科学计算
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&am ...
- (转)IO复用,AIO,BIO,NIO,同步,异步,阻塞和非阻塞 区别
本文来自:https://www.cnblogs.com/aspirant/p/6877350.html?utm_source=itdadao&utm_medium=referral,非常感谢 ...
- Drools规则引擎环境搭建
Drools 是一款基于Java 的开源规则引擎,所以在使用Drools 之前需要在开发机器上安装好JDK 环境,Drools5 要求的JDK 版本要在1.5 或以上. Drools5 提供了一个基于 ...