I set up the following service collection with a Refit client and a Polly policy for dependency injection in my UWP app:
var serviceCollection = new ServiceCollection();
serviceCollection
.AddRefitClient(typeof(IClassevivaAPI))
.ConfigureHttpClient(
(sp, client) =>
{
client.BaseAddress = new Uri(Endpoint.CurrentEndpoint);
}
)
.AddPolicyHandler(
Policy<HttpResponseMessage>
.HandleResult(r => r.StatusCode == System.Net.HttpStatusCode.Unauthorized)
.RetryAsync(
1,
async (ex, count) =>
{
Debug.WriteLine("Retry {0} times", count);
var loginCredentials = new CredUtils().GetCredentialFromLocker();
if (loginCredentials != null)
{
//the retry can continue
}
}
)
);
When the StatusCode of the http response is 401 (which in the API I'm using means that the session has expired) the app checks whether credentials have been stored in my app (in my app at the login stage I can choose if this is a temporary session without saving the credentials) or not.
If credentials are stored the Polly policy can continue retrying the request.
But I want that in case of credentials not stored the policy doesn't execute the retry but instead closes the app.
Is there a way to tell the Polly policy to execute the retry conditionally when handling that specific http event without hacky tricks?