GithubHelp home page GithubHelp logo

Comments (2)

martincostello avatar martincostello commented on June 16, 2024 1

Looking at your code, my guess is that the operation fails 6 times in a row, so you see the exception bubbling up from the sixth attempt because you're configured to allow for 5.

If you never want to see an exception thrown, you would also need to include a fallback policy to provide a value to use instead. Though for your use case of getting an access token, I assume there's nothing you can really provide as a fallback in that case.

from polly.

HossZamani avatar HossZamani commented on June 16, 2024

Thanks for the pointer. That WAS the issue. I updated my code like below and it made it work.

private static AuthenticationResult GetNewToken(string token, string refreshToken, out API_AuthenticationResult API_result)
{
    var fallBackOption = new FallbackStrategyOptions<API_Response<API_AuthenticationResult>>
    {
        ShouldHandle = new PredicateBuilder<API_Response<API_AuthenticationResult>>()
            .Handle<Exception>(),
        FallbackAction =  args => 
            {
                return Outcome.FromResultAsValueTask(new API_Response<API_AuthenticationResult>()
                {
                    Code = ResponseCode.UserNotExist405,
                    Message = "Please login again."
                });
            },
        OnFallback = args =>
            {
                return default;
            }
            
    };

    var retryOptions = new RetryStrategyOptions<API_Response<API_AuthenticationResult>>
    {
        Delay = TimeSpan.FromSeconds(1),
        MaxRetryAttempts = 5,
        ShouldHandle = new PredicateBuilder<API_Response<API_AuthenticationResult>>()
            .Handle<Exception>()
    };
    var pipelineBuilder = new ResiliencePipelineBuilder<API_Response<API_AuthenticationResult>>();
    pipelineBuilder.AddFallback<API_Response<API_AuthenticationResult>>(fallBackOption);
    pipelineBuilder.AddRetry<API_Response<API_AuthenticationResult>>(retryOptions);

    var pipeline = pipelineBuilder.Build();

    var result = pipeline.Execute(() =>
    {
        var body = new
        {
            Token = token,
            RefreshToken = refreshToken
        };
        var res = DataAccess.RestAPI_GetData<API_AuthenticationResult>(CreateRibbon.Application.AppSettings.ApiBaseAddress,
                  API_Endpoints.RefreshToken,
                  null,
                  body: body);

        if (res == null)
        {
            throw new Exception("API response is null");
        }
        if (res != null && res.Code != ResponseCode.Success201)
        {
            throw new Exception(res.Message);
        }
        return res;
    });


    return ProcessAuthentication(result, out API_result);
}```

from polly.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.