【问题标题】:Retry Interceptor using Polly is not working for Unity使用 Polly 重试拦截器不适用于 Unity
【发布时间】:2019-04-02 10:22:10
【问题描述】:

我正在使用 Microsoft Unity v5.8.6 和 Polly v7.1.0。 我的要求是我需要基于一键触发电子邮件。出于某种原因,如果 sendEmail 失败,我需要重试“x”次。

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;
    }

}

我在 NotificationService.cs 类中用属性修饰了 sendMail 方法

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

统一配置

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

除Policy.Handle&lt;Exception&gt;().WaitAndRetry 外,一切都按预期工作。发生异常时,它不会重试,而是返回结果。 我不确定我错过了什么。

提前致谢

【问题讨论】:

    标签: c# unity-container polly


    【解决方案1】:

    您需要在 polly 的 .Execute 处理程序中重新引发异常。请尝试以下代码

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

    【讨论】:

      猜你喜欢
      • 2013-11-07
      • 2019-02-22
      • 1970-01-01
      • 2021-10-20
      • 2015-02-23
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 1970-01-01
      相关资源
      最近更新 更多