I have set up my Xamarin core project with a lot of the tips I read from here.
I have a simple service that I'm using to see if an entered url for a server is valid: IValidateUrlService
I'm trying to use the Polly library to do something like this:
var request = new PingUrlRequest();
var validateUrlTask = apiService.UserInitiated.ValidateUrl(request);
var response = await Policy
.Handle<ApiException>()
.RetryAsync(retryCount: 5)
.ExecuteAsync(async () =>
await validateUrlTask);
reachable = HandleServerResponse(response);
This works fine unless I get an Exception. For instance, when I was getting an ApiException on one server I was testing against; the above code did not absorb the error! It just threw the exception and crashed the program. I could only get the code to work properly by doing this:
try
{
var request = new PingUrlRequest();
var validateUrlTask = apiService.UserInitiated.ValidateUrl(request);
var response = await Policy
.Handle<ApiException>()
.RetryAsync(retryCount: 5)
.ExecuteAsync(async () =>
await validateUrlTask);
reachable = HandleServerResponse(response);
}
catch (ApiException e)
{
Mvx.Trace(e.StackTrace);
}
But that doesn't feel correct... What's the point of having the Handle parameter up top if it doesn't do anything? I have to be implementing this wrong! Any suggestions?
If ApiException
is being thrown that means that Polly retried 5 times to execute your code and failed each time thus it all ended with ApiException
and you need to handle it.
What's the point of having the Handle parameter up top if it doesn't do anything?
Handle specifies which type of exception your policy should apply for so you used it appropriately.
But that doesn't feel correct...
I use the same pattern as you did in 2 code snippet and it is ok imho.
Hope this helps!