-1

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

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
User123
  • 19
  • 3
  • Based on your description it seems like Service Not Available and Internal Server Error are status codes (not exceptions). Is my understanding correct? – Peter Csala Jul 26 '23 at 06:36
  • Yes, atleast 500(Internal Server Error) are all the http status code – User123 Jul 26 '23 at 07:09

2 Answers2

0

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 this SqlException and Win32Exception:

.Handle<SqlException>(e => SqlHelper.ShouldRetryOn(e))
.Or<TimeoutException>()
.OrInner<Win32Exception>(Win32Helper.ShouldRetryOn)


public static bool ShouldRetryOn(Exception ex)
{
    StringBuilder sb = new();
    if (ex is SqlException sqlException)
    {
        foreach (SqlError err in sqlException.Errors)
        {
            switch (err.Number)
            {




public static bool ShouldRetryOn(Win32Exception ex)
{
    switch (ex.NativeErrorCode)
    {
        // Timeout expired
        case 0x102:
            return true;
        // Semaphore timeout expired
        case 0x121:
            return true;
        default:
            return false;
    }
}

Alternatively tap into Pollys in built WaitAndRetry for Http calls HttpPolicyExtensions.HandleTransientHttpError().WaitAndRetryAsync

Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • `HttpPolicyExtensions.HandleTransientHttpError().WaitAndRetryAsync` will not work on customOutput type dont know why If you give me an example how to implement this, it would be really helpful – User123 Jul 26 '23 at 07:10
  • @User123 grab an example from github https://github.com/search?type=code&q=HandleTransientHttpError%28%29 – Jeremy Thompson Jul 27 '23 at 00:52
0

The Handle and Or builder methods help you to define trigger for exceptions.

The HandleResult and OrResult builder methods help you to define trigger based on the response/result object.

Policy
  .HandleResult<CustomResponse>(res => res.StatusCode == HttpStatusCode.ServerInternalError)
  .Or<HttpRequestException>()
  .Or<TimeOutexception()
  .Or<BadRequestException>()

OR

Policy
  .Handle<HttpRequestException>() 
  .OrResult<CustomResponse>(res => res.StatusCode == HttpStatusCode.ServerInternalError)
  .Or<TimeOutexception()
  .Or<BadRequestException>()

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.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
  • Hi, thank you for your response but with respect to CustomResponse, i do not have status code property, it only contains the data i get from getResponse method. which will have data only when call is success else it will be null. – User123 Jul 26 '23 at 09:14
  • 1
    @User123 If you don't have access to the status code then how do you want to define a policy which should trigger for specific status codes? – Peter Csala Jul 26 '23 at 10:17