Answer the question
In order to leave comments, you need to log in
How to make a method call again?
Hello everyone, I have some code like this
public async Task<List<AccountListView>> GetAsync()
{
try
{
return await TryGetAccountListAsync();
}
catch (UnauthorizedException)
{
await Context.Renew();
return await TryGetAccountListAsync();
}
}
private async Task<List<Model>> TryGetAccountListAsync()
{
var response = /*Запрос к серверу*/
if(!response.IsSuccessStatusCode) {
throw new UnauthorizedException();
} eles {
return response.ToModel();
}
}
Answer the question
In order to leave comments, you need to log in
If you want "clear and beautiful so that it's no worse than the boys", take a look at the Polly lib :
public async Task<List<AccountListView>> GetAsync()
{
var policy = Policy
.Handle<UnauthorizedException>()
.RetryAsync(
retryCount: 1,
onRetryAsync: (e, i) => Context.Renew());
return await policy.ExecuteAsync(TryGetAccountListAsync);
}
policy
can be reused. Those. you don’t have to copy-paste anymore, just declare one policy per application, and then just call what requires updating the token through this policy.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question