Open sndpyadav34 opened 6 days ago
Here's some code that uses different policies depending on the HTTP method.
Two different policies are added to a policy registry, one for "reads" and one for "writes": https://github.com/martincostello/alexa-london-travel-site/blob/5dc4c38445a6d7e1bb445898f5523b8b5f94eb0f/src/LondonTravel.Site/Extensions/PollyServiceCollectionExtensions.cs
The appropriate policy is then obtained from the registry based on the HTTP method of the current request: https://github.com/martincostello/alexa-london-travel-site/blob/0a6829fc01976f673cf9f26bcbf0dae36d72199a/src/LondonTravel.Site/Extensions/IHttpClientBuilderExtensions.cs#L90
The AddPolicyHandler
call registers a PolicyHttpMessageHandler
which is a DelegatingHandler
. I would suggest to combine all your policies into a single Policy.WrapAsync
and register only a single DelegatingHandler
instead of three.
.AddPolicyHandler(Policy.WrapAsync(retryForGet, retryForNonGet, circuitBreaker, perRequestTimeout));
What are you wanting to achieve?
I have a common
HttpClient
that is used or sending multiple requests like GET, POST, PUT etc. I am trying to configure different retry behavior for GET versus POST/PUT calls. Basically I am trying to configure a timeout per retry behavior for GET calls and a transient error behavior for POST/PUT calls.What code or approach do you have so far?
I defined following two policies.
policy1
handles the result of GET requests and handles theTimeoutRejectedException
along with transient errors whereaspolicy2
handles transient errors for requests other than GET. I then wrap both policies.And then while registering the
HttpClient
I add the retry policy handlers as below:However, with the policies configured like this, I see retries happening for both GET and POST requests. I think it's because of the timeout policy around the HttpClient. Not sure if it's possible to define on a single client like this.
Additional context
No response