系统里面常常调用服务和读写文件里面需要对发生异常,操作失败时进行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 = )
{
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 = )
{
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 = )
{
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 = )
{
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 = )
{
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 = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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 = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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 = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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 = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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 = )
{
var exceptions = new List<Exception>(); for (int retry = ; 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通用类的更多相关文章

  1. 【转载】C#异常Retry通用类

    //Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...

  2. C#异常重试通用类Retry

    //Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...

  3. Util应用程序框架公共操作类(五):异常公共操作类

    任何系统都需要处理错误,本文介绍的异常公共操作类,用于对业务上的错误进行简单支持. 对于刚刚接触.Net的新手,碰到错误的时候,一般喜欢通过返回bool值的方式指示是否执行成功. public boo ...

  4. 使用C# 操作存储过程,执行sql语句通用类

    如何使用C# 操作存储过程,执行sql语句? 闲话不多说,直接上代码:     /// <summary>    /// Sql通用类    /// </summary>    ...

  5. Django子应用里的通用类混合视图处理

    # 子应用的views.py此文件用于编写Web应用视图,处理复杂的业务逻辑 #导入方法视图模板 from django.shortcuts import render #导包 from django ...

  6. poi导出excel通用类

    一.关键的通用类public class PoiExportUtils {    private static HSSFWorkbook workBook; public PoiExportUtils ...

  7. NPOI MVC 模型导出Excel通用类

    通用类: public enum DataTypeEnum { Int = , Float = , Double = , String = , DateTime = , Date = } public ...

  8. MVC NPOI Linq导出Excel通用类

    之前写了一个模型导出Excel通用类,但是在实际应用中,可能不是直接导出模型,而是通过Linq查询后获取到最终结果再导出 通用类: public enum DataTypeEnum { Int = , ...

  9. NPOI导入导出EXCEL通用类,供参考,可直接使用在WinForm项目中

    以下是NPOI导入导出EXCEL通用类,是在别人的代码上进行优化的,兼容xls与xlsx文件格式,供参考,可直接使用在WinForm项目中,由于XSSFWorkbook类型的Write方法限制,Wri ...

随机推荐

  1. 使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空

    使用mysql 命令行,增加 ,删除 字段 并 设置默认值 及 非空 添加 alter table table_name add field_name field_type; 添加,并设置默认值,及非 ...

  2. sqlserver索引与查询优化

    此文为转载,仅做保存使用,出处:http://www.cr173.com/html/8688_all.html 在数据库存优化设计中往往会提到索引,这编文章就来详细的说明一下在 SQL SERVER ...

  3. mysql修改为utf8格式

    1.修改相应数据库的编码格式: -- 修改数据库编码为utf8 ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_general_ ...

  4. django搭建论坛之一环境配置

    环境情况: Description:    Ubuntu 15.04   Python版本:   Python 2.7.9 django版本: 1.8.4 安装django: pip install ...

  5. Hibernate QBC运算符

    HQL运算符 QBC运算符 含义 = Restrictions.eq() 等于equal <> Restrictions.ne() 不等于not   equal > Restrict ...

  6. ebtables hook

    1 概述 netfliter框架不仅仅在ipv4中有应用,bridge,ipv4,ipv6,decnet 这四种协议中都有应用,其中ipv4中又分开了arp和ip的两种 其实netfliter是个大的 ...

  7. IOS XML解析

    <?xml version = "1.0"  encoding ="utf-8"?> <video>小黄人</video> ...

  8. ubuntu命令大全

    一.文件/文件夹管理 ls 列出当前目录文件(不包括隐含文件) ls -a 列出当前目录文件(包括隐含文件) ls -l 列出当前目录下文件的详细信息 cd .. 回当前目录的上一级目录 cd - 回 ...

  9. js设置本周 本月 本年

    var SetSearchDate = function (sign, sid, eid) {//sign 标识符区分本周本月本年,sid开始时间id,eid结束时间id var now = new ...

  10. [转载]centos7 快速安装 mariadb(mysql)

    http://blog.csdn.net/default7/article/details/41973887 从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! yum ins ...