With Polly I'd like to have my policy definition and the execution of that policy in two different statements, as in:
// Policy definition
var policy = Policy
.HandleResult<IRestResponse>(predicate)
.Retry(2);
// Policy execution
policy.ExecuteAndCapture(() =>
{
DoSomethingAndReturnAnIRestResponse();
};
I want to do it this way so I can get better reuse of my retry policies, e.g. for use in dependency injection.
I'm trying to understand if there's any considerations when splitting up the policy and the execution in this way, for example if there's any "state" (for lack of a better term) that might not carry in the policy
object from policy definition to execution.
Along these lines I'm noticing that when I use Polly's ExecuteAndCapture()
in the above fashion that certain properties (those related to capturing the final exception/outcome, associated with ExecuteAndCapture()
) aren't showing on the policy
object. Per the documentation (here and here), upon completion of a policy such as the below:
var policy = Policy
.HandleResult<IRestResponse>(predicate)
.Retry(2)
.ExecuteAndCapture(() =>
{
DoSomethingAndReturnAnIRestResponse();
});
...you are supposed to get back:
PolicyResult.Outcome
PolicyResult.FinalException
PolicyResult.ExceptionType
PolicyResult.Result
This indeed is happening then the ExecuteAndCapture()
is in the same statement as the policy definition. However, when separating the policy definition from the execution those properties aren't available. I naively assumed they'd show up on the existing policy
object, but they don't:
It seems I need to create a new variable assignment in order to access to those properties:
Any concerns here?