protected override object Invoke(MethodInfo targetMethod, object[] args) { object result = null; var session = new RetrySession(_retryPolicy) .Begin(); Exception exception = null; var complete = false; while (!complete) { try { var isAwaitable = targetMethod.ReturnType.GetMethod(nameof(Task.GetAwaiter)) != null; result = isAwaitable ? InvokeAsynchronous(targetMethod, args) : InvokeSynchronous(targetMethod, args); //result = targetMethod.Invoke(_instance, args); complete = true; } catch (TargetInvocationException ex) { exception = ex.GetPrimaryException(); } catch (AggregateException ex) { exception = ex.GetPrimaryException(); } catch (Exception ex) { throw new RetryException(session, $"Unexpected exception type '{ex.GetType().FullName}'. See inner exception for details.", ex); } // test the exception for can retry, exceeded max retry, and timeout if (exception != null) { var retryException = session.CheckException(exception); if (retryException != null) { throw retryException; } // wait a bit before trying again session.Sleep(); //reset exception for next attempt exception = null; } } // while (!complete) session.End(); return(result); }
public RetryException(RetrySession retrySession) : base() { RetrySession = retrySession ?? throw new ArgumentNullException(nameof(retrySession)); RetrySession.End(); }
public RetryException(RetrySession retrySession, string message, Exception innerException) : base(message, innerException) { RetrySession = retrySession ?? throw new ArgumentNullException(nameof(retrySession)); RetrySession.End(); }