using System; using System.Threading; using System.Diagnostics; using System.Reflection; using System.Windows.Forms; using System.Collections.Generic; using System.IO; using System.ComponentModel; using System.Collections; using System.Text; using System.Runtime.Serialization.Formatters.Binary; using BrainTechLLC.ThreadSafeObjects; using LinFu.AOP; using LinFu.AOP.Interfaces; namespace BrainTechLLC { public class RetryMethodReplacement : BaseMethodReplacementProvider, IMethodReplacement { public override bool ShouldReplace(IInvocationContext context) { RetryAttribute[] attrs = context.TargetMethod.ReadAttribute<RetryAttribute>(); return (attrs != null && attrs.Length > 0); } protected override IMethodReplacement GetReplacement(IInvocationContext context) { return this; } public override object Invoke(IInvocationContext context) { RetryAttribute[] attrs = context.TargetMethod.ReadAttribute<RetryAttribute>(); if (attrs == null || attrs.Length == 0) return context.TargetMethod.Invoke(context.Target, context.Arguments); for (int n = 0; n < attrs.Length; n++) { RetryAttribute attr = attrs[n]; int tries = 0; while (tries < attr.RetryTimes) { try { return context.TargetMethod.Invoke(context.Target, context.Arguments); } catch { tries++; if (tries > attr.RetryTimes) throw; if (attr.SleepBetweenRetryMS >= 0) Thread.Sleep(attr.SleepBetweenRetryMS); } } } return null; } } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)