title: C# retry method interception with free a AOP framework
body: <p>I'm totally new to the AOP in C#. Currently, I'm interested to write a retry for a method that uses this technique.</p>
  
  <p>So, based on the postsharp tut: <a href="https://doc.postsharp.net/method-interception" rel="nofollow noreferrer">https://doc.postsharp.net/method-interception</a> 
  I've written this code:</p>
  
  <pre><code>[Serializable]
      public class RetryAspect : MethodInterceptionAspect
  {
      private int _sleep;
      private int _retries;
      private object _expectedResult;
      private object _defaultReturnValue;
  
      public RetryAspect(object expectedResult, int waitBetweenCycles, int numberOfRetries) : this(expectedResult, waitBetweenCycles, numberOfRetries, null) { }
  
      public RetryAspect(object expectedResult, int waitBetweenCycles, int numberOfRetries, object defaultReturnValue)
      {
          _expectedResult = expectedResult;
          _sleep = waitBetweenCycles;
          _retries = numberOfRetries;
          _defaultReturnValue = defaultReturnValue;
      }
  
      public override void OnInvoke(MethodInterceptionArgs args)
      {
          int waitCount = 0;
  
          while (!args.ReturnValue.Equals(_expectedResult))
          {
              args.Proceed();
  
              if (waitCount++ &lt; _retries)
              {
                  Thread.Sleep(_sleep);
              }
              else
              {
                  if (_defaultReturnValue != null)
                  {
                      args.ReturnValue = _defaultReturnValue;
                  }
  
                  break;
              }
          }
      }
  }
  
  class Program
  {
      static int cnt = 0;
  
      static void Main(string[] args)
      {
          Console.WriteLine(Test());
          Console.ReadKey();
      }
  
      [RetryAspect(true, 1000, 5)]
      public static bool Test()
      {
              Console.WriteLine("Test {0}", cnt);
              if (cnt == 4)
              {
                  return true;
              }
              else
              {
                  cnt++;
                  return false;
              }
      }
  }
  </code></pre>
  
  <p>Now, is there a way to achieve the same result by using a free/open source AOP framework? So far, I didn't found any useful example with a different AOP framework.</p>