惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
SegmentFault 最新的问题
G
Google Developers Blog
Stack Overflow Blog
Stack Overflow Blog
WordPress大学
WordPress大学
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
罗磊的独立博客
月光博客
月光博客
IT之家
IT之家
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
小众软件
小众软件
C
Check Point Blog
B
Blog RSS Feed
H
Help Net Security
博客园 - 司徒正美
L
LangChain Blog
MongoDB | Blog
MongoDB | Blog
B
Blog
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Security Blog
Microsoft Security Blog
M
MIT News - Artificial intelligence
N
Netflix TechBlog - Medium
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - 留云

dotnet core 自定义配置文件 Dotnet Core Windows Service golang 用tar打包文件或文件夹 golang 最和谐的子序列 golang 轮训加密算法 golang map golang 队列 golang 栈操作 golang 多维数组 golang 数组反转 nginx 在windows平台上对asp.net做反向代理 - 留云 树形结构应用技巧 用批命令更新数据库 jquery 调用wcf project HTTP协议之Session和Cookie C#多线程同步 SQL优化-索引 UML在关系型数据库设计中的应用 Web 应用的 UML 建模与 .NET 框架开发
c# 中Exception 的重试机制
留云 · 2011-05-23 · via 博客园 - 留云

有时候我们在项目中碰到,比如这样那样的原因(网络或者是其他的原因)导致方法调用失败,大部分的方法是在触发一次方法调用,其实我们可以通过自定义Exception,然后提供调用的方法名称和参数等等,提供一种自动的重试多次的机制。

其实方法是很简单的,我们先自定义一个Exception

代码如下



 public class RetryException : Exception
    {
        private int RetryTimes;
        private string MethodName;
        private object[] Values;
        private Type ClassType;
        public RetryException(int retryTimes, Type classType, string method, params object[] values)
        {
            RetryTimes = retryTimes;
            MethodName = method;
            Values = values;
            ClassType = classType;
            RetryMethod();
        }
        public RetryException(string message, int retryTimes, Type classType, string method, params object[] values)
            : base(message)
        {
            RetryTimes = retryTimes;
            MethodName = method;
            ClassType = classType;
            Values = values;
            RetryMethod();
        }
        public RetryException(string message, Exception innerException, int retryTimes, Type classType, string method, params object[] values)
            : base(message, innerException)
        {
            RetryTimes = retryTimes;
            MethodName = method;
            ClassType = classType;
            Values = values;
            RetryMethod();
        }
        private void RetryMethod()
        {
            if (string.IsNullOrEmpty(MethodName))
                throw new Exception("method 为空");
            if (RetryTimes == 0)
                throw new Exception("重试次数为空");
            if (ClassType == null)
                throw new Exception("ClassType为空");
            object instance = Activator.CreateInstance(ClassType);
            MethodInfo Method = ClassType.GetMethod(MethodName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public);
            do
            {
                try
                {   
                    Method.Invoke(instance, Values);
                }
                catch (Exception ex)
                {
                    if (RetryTimes > 1)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    else
                    {
                        throw ex;
                    }
                }
                RetryTimes--;
            } while (RetryTimes > 0);
        }
    }

使用方法 我们先定义一个简单的方法如下

 public class service
    {
        public void GetName(string message,object obj)
        {
            if (message == "error")
                throw new Exception(" message");
               
        }
    }

调用的方法是

  service sv = new service();
            object obj = new object();
            try
            {
                sv.GetName("error",obj);
            }
            catch (Exception ex)
            {
               new RetryException(5, typeof(service), "GetName", new object[] {"error",obj});
            }