I am calling an external API and want to deal with the event that the call returns Unauthorized HttpResponseMessage. When this happens, I want to update the access token and make the call again.
I am trying to use Pollywith the following code:
public async Task<HttpResponseMessage> MakeGetRequestAsync()
{
var retryPolicy = Policy
.HandleResult<HttpResponseMessage>(r => r.StatusCode == HttpStatusCode.Unauthorized)
.Retry(1, (exception, retryCount) =>
{
RefreshAccessToken();
});
var result = await retryPolicy.ExecuteAsync(() => CallApiAsync());
return result;
}
private async Task<HttpResponseMessage> CallApiAsync()
{
var url = Options.ResourceSandboxUrl;
var httpClient = new HttpClient();
SetRequestHeaders(httpClient);
var response = await httpClient.GetAsync(url);
response.StatusCode = HttpStatusCode.Unauthorized;
return response;
}
I set breakpoints in the statement ExecuteAsyncand in DoSomethingAsync- when passing through ExecuteAsync DoSomethingAsyncit is not called and control returns to a function calledMakeGetRequestAsync
I donβt understand why itβs DoSomethingAsyncnot called - can someone help me with what I am trying to achieve?
I looked at the Polly documentation and Polly's questions about the stack overflow, but I can't figure out what is going on.