I am trying to get the timeout policy to work correctly. I have the following requirements while integrating an api.
- Create an http request to invoke endpoint1 and pass the transactionID and capture the result
- if the http request does not receive an answer in 20 seconds then send a cancel request with the same transactionID and capture the result
For this task I would like to use Polly which seems to me a fantastic component to help handling transient failures. However as I am very new to this technology I just want to be sure if I am implementing correctly.
First of all I have created a timeout policy with Polly like this
var timeoutPolicy =
Policy.TimeoutAsync(
TimeSpan.FromSeconds( 20 ),
TimeoutStrategy.Optimistic,
async ( context, timespan, task ) => {
//write here the cancel request
} );
then after that I am ready to execute the policy
var policyResult = await timeoutPolicy.ExecuteAndCaptureAsync( async () => {
//make here the request 1
} );
What I got from the documentation is that if a timeout occurs inside the timeoutPolicy.ExecuteAndCaptureAsync
delegate Polly automagically invoke the onTimeout
delegate. Right?
However my questions are:
- What happens if inside the execute delegate an exception occurs? Should I wrap that polly construct in a try catch?
- When I analyze the policy result how do I understand if the timeout has happened or not?
policyResult
variable I should use aResult
member but this is not available to me. – Pharyngo