C# 有try-catch ,但是没有retry 功能,通过用有限次循环的办法来模拟Retry,当然中间需要加一个等待的过程。

我们可以利用C#的匿名方法(anonymous methods)匿名委托(anonymous delegate)修饰此功能

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading; namespace Exchange.Common
{
public class ActionExecutor
{
/// <summary>
///
/// </summary>
/// <typeparam name="T1"></typeparam>
/// <param name="action"></param>
/// <param name="arg1"></param>
/// <param name="customerName"></param>
/// <param name="actionName"></param>
/// <param name="poNumber"></param>
/// <param name="retryCount"></param>
public static void Excute<T1>(Action<T1> action, T1 arg1, string customerName, string actionName, string poNumber, int retryCount = )
{
Excute<T1>(action, arg1, customerName, actionName, poNumber, new TimeSpan(, , ));
}
/// <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 void Excute<T1>(Action<T1> action, T1 arg1,string customerName,string actionName, string poNumber, TimeSpan retryInterval, int retryCount = )
{
//var exceptions = new List<Exception>(); for (int retry = ; retry < retryCount; retry++)
{
try
{
action(arg1);
return;
}
catch (Exception ex)
{
ExceptionHandler.Do(string.Format("{0} {1} \"{2}\" error", customerName, actionName, poNumber, retry), ex);
//exceptions.Add(ex);
Thread.Sleep(retryInterval);
}
}
}
}
}

在其他类中调用

ActionExecutor.Excute(RequesetOrder, order,
CUSTOMER_NAME,
"request order detail", order.OrderNumber); //RequesetOrder 类方法
//order 是RequesetOrder的参数

C# 实现retry的更多相关文章

  1. 第3月第21天 nsclassfromstring返回null SVN报错:clean the working copy and then retry the operation

    1. xcodeproj工程损坏时,.m文件没有加入编译. 2. SVN报错:clean the working copy and then retry the operation http://bl ...

  2. -bash: fork: retry: Resource temporarily unavailable

    登陆不了服务器The server refused to start a shell. 登陆服务器后执行ls命令报错:   1 2 $ls -bash: fork: retry: Resource t ...

  3. java function retry wrapper

    import java.util.concurrent.Callable; /** * Created by huahui.yang on 1/29/16. */ public class Retry ...

  4. Spring retry基本使用

    Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...

  5. freebsd启动报错:My unqualified host name unkown...Sleeping for retry.

    原文 http://blog.163.com/sujoe_2006/blog/static/335315120111158576591/ 病状:启动报"My unqualified host ...

  6. ebs R12.2启动报错"failed to start a managed process after the maximum retry limit"

    启动日志: Error --> Process (index=1,uid=1739599208,pid=4479) failed to start a managed process after ...

  7. Windows Azure Storage 之 Retry Policy (用来处理短暂性错误-Transient Fault)

    在使用Windows Azure Storage Service 的时候, 通常会遇到各种各样的问题. 例如网络连接不稳定,导致请求没有发出去.删除一个Blob Container 之后又立刻创建同名 ...

  8. 启动受管服务器出现:unable to get file lock, will retry...

    启动受管服务器出现:unable to get file lock, will retry... 解决方法:一.删掉Domain下的*.lok文件1. 删除edit.lok进入到domain_home ...

  9. spring retry 使用

    1.  场景      系统方法调用时无状态的,同时因为网络原因,或者系统暂时故障,进行的重试 2. maven 依赖 <project xmlns="http://maven.apa ...

  10. C# Retry重试操作解决方案(附源码)

    一.前言 (1)对于Thread的Abort方法,如果线程当前正在执行的是一段非托管代码,那么CLR就不会抛出ThreadAbortException,只有当代码继续回到CLR中时,才会引发Threa ...

随机推荐

  1. 线性表的链式存储——C语言实现

    SeqList.h #ifndef _WBM_LIST_H_ #define _WBM_LIST_H_ typedef void List; typedef void ListNode; //创建并且 ...

  2. PowerDesigner的CDM模型将低驼峰命名法则的每个大写字母前加_符

    Option   Explicit ValidationMode   =   True InteractiveMode =   im_Batch Dim   mdl   '当前model '获取当前活 ...

  3. SqlServer——判断对象是否存在

    对以下对象判断是否存在:database.table.proc.触发器.临时表.索引.对于这些对象的判断是通过数据表 SysObjects来获得的. 一.基础知识 1.SysObjects系统表 对于 ...

  4. 【263】Linux 添加环境变量 & 全局 shell 脚本

    Linux电脑添加环境变量 方法一:通过修改 profile 文件添加环境变量 1. 打开终端,输入[vi /etc/profile],如下所示,点击回车 [ocean@ygs-jhyang-w1 L ...

  5. Python单例模式剖析

    在聊这之前我们首先要明确的是,单例模式在实际中的意义以及在python中具有实现的价值? 当前,相信有很多人支持单例模式,也有不少人反对,尤其是在python中,目前依旧具有很大的争议性.我们要在评论 ...

  6. redis的特性

  7. 202. Happy Number 平方循环数

    [抄题]: Write an algorithm to determine if a number is "happy". A happy number is a number d ...

  8. 长城防火墙(GFW)

    一.简介 中国防火长城,官方名为金盾工程,是由政府运作的一个互联网审查监控项目.在其管辖互联网内部建立的多套网络审查系统的总称,包括相关行政审查系统.其英文名称Great Firewall of Ch ...

  9. Luogu 4216 [SCOI2015]情报传递

    BZOJ 4448. 写起来很愉悦的题. 按照时间可持久化线段树,修改就在原来的地方加$1$即可,查询就直接询问$root_1 - root_{now - c - 1}$中相应的个数. 主席树维护树链 ...

  10. DWR原理探秘

    DWR原理探秘 DWR(Direct Web Remoting)远程Web命令;是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX技术的网站.它可 ...