I am trying to implement polly retry for a method for http transient exception but i am stuck on issues
here is the situation
CustomResponse response = getResponse(CustomRequest request)
getResponse
has 2 implemtation one using wcf another using API (These2 implementation are in separate class)
If succeed i will get a response of my output type CustomResponse
Or it will throw exception such as serviceNotAvailable or Internal server error
I tried writing this below policy
Policy. handle<httpRequestException>()
.Or<TimeOutexception()
.Or<BadRequestException>()
But i am not able to get one for service not available and internal server error
2
Answers
What you’ll need to do is wire it up yourself. Here is an example with SQL and Win23 Errors, some errors such as Connection Failures I don’t retry, others I return true in order to retry. Change this code to tap into the
HttpRequestException
in a similar way to thisSqlException
andWin32Exception
:Alternatively tap into Pollys in built WaitAndRetry for Http calls
HttpPolicyExtensions.HandleTransientHttpError().WaitAndRetryAsync
The
Handle
andOr
builder methods help you to define trigger for exceptions.The
HandleResult
andOrResult
builder methods help you to define trigger based on the response/result object.OR
The above code samples assumed that the StatusCode is accessible through the
CustomResponse
object.These samples can be easily extended to trigger for multiple statuscodes.