Search code examples
c#unity-containerpolly

Retry Interceptor using Polly is not working for Unity


I am working with Microsoft Unity v5.8.6 and Polly v7.1.0. My requirement is I need to trigger email based on one buttonclick. For some reason if sendEmail fails I need to retry for 'x' times.

RetryOnExceptionAttribute.cs

[AttributeUsage(AttributeTargets.All, AllowMultiple = true, Inherited = true)]
public class RetryOnExceptionAttribute : Attribute
{
    public int MaxAttempts { get; set; }
    public int RetryDelay { get; set; }

    public RetryOnExceptionAttribute(int maxAttempts,int retryDelay)
    {
        MaxAttempts = maxAttempts;
        RetryDelay = retryDelay;
    }

}

RetryInterceptor.cs

public class RetryInterceptor : IInterceptionBehavior
{
    public bool WillExecute { get { return true; } }

    public RetryInterceptor()
    {

    }
    public IEnumerable<Type> GetRequiredInterfaces()
    {
        return Type.EmptyTypes;
    }

    public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    {
        var attrClass = input.MethodBase.GetCustomAttributes(typeof(RetryOnExceptionAttribute), true);
        RetryOnExceptionAttribute retryOnException;
        IMethodReturn result = null;
        if (!attrClass.Any())
        {
            result = getNext()(input, getNext);
            return result;
        }
        else if (attrClass.Any())
        {
            try
            {
                retryOnException = (RetryOnExceptionAttribute)attrClass[0];
                int maxAttempsts = retryOnException.MaxAttempts);
                int maxRetryDelay = retryOnException.RetryDelay;
                Policy.Handle<Exception>().WaitAndRetry(maxAttempsts, retryAttempt => TimeSpan.FromSeconds(maxRetryDelay), (exception, timespan, retryCount, context) =>
                {
                    Console.WriteLine($"Class: {input.Target}, Method: {input.MethodBase}, Retry Count:{retryCount}, Exception {exception.GetCompleteMessage()}");

                }).Execute(() => { result = getNext()(input, getNext); }); /*I am thinking something needs to be changed in Execute() method*/
            }
            catch (Exception)
            {

            }
        }

        return result;
    }

}

I have decorated sendMail method in NotificationService.cs class with attribute

 [RetryOnException(3, 1)]
    public void SendEmail(NotificationRequest request)
    {

UnityConfiguration

container.RegisterType<INotificationService, Services.NotificationService>(new TransientLifetimeManager(),new Interceptor<InterfaceInterceptor>(), new InterceptionBehavior<RetryInterceptor>());

Everything is working as expected except Policy.Handle<Exception>().WaitAndRetry. When the exception occured it is not retrying instead it is returning the result. I am not sure what I am missing.

Thanks in Advance


Solution

  • You need to rethrow the exception in the polly's .Execute handler. Please try the following the code

      .Execute(() => {
           result = getNext()(input, getNext);
           if (result.Exception != null) {
              throw new Exception("Retry", result.Exception);
           }
      });