Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. var generalExceptionPolicy=Policy.Handle<Exception>().WaitAndRetry(2, retryAttempt =>
  2. TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),(exception,timespan)=>{
  3.  
  4. if(attempt++ == 2)
  5. {
  6. Toast.MakeText(Activity,"No Connection Bro",ToastLength.Short).Show();
  7.  
  8.  
  9. }
  10. });
  11. var test = await generalExceptionPolicy.ExecuteAsync(()=>PostPreLogin (view.FindViewById<EditText> (Resource.Id.mobileTextBox).Text));
  12.  
  13. public async Task<string> PostPreLogin(string userName)
  14. {
  15. var preloginvalue = await Account.PreLoginPost (userName);
  16. return preloginvalue;
  17.  
  18. }
  19.  
  20. generalExceptionPolicy.ExecuteAndCapture(() => DoSomething());
  21.  
  22. async Task<HttpResponseMessage> QueryCurrencyServiceWithRetryPolicy(Func<Task<HttpResponseMessage>> action)
  23. {
  24. int numberOfTimesToRetry = 7;
  25. int retryMultiple = 2;
  26.  
  27. //Handle HttpRequestException when it occures
  28. var response = await Policy.Handle<HttpRequestException>(ex =>
  29. {
  30. Debug.WriteLine("Request failed due to connectivity issues.");
  31. return true;
  32. })
  33.  
  34. //wait for a given number of seconds which increases after each retry
  35. .WaitAndRetryAsync(numberOfTimesToRetry, retryCount => TimeSpan.FromSeconds(retryCount * retryMultiple))
  36.  
  37. //After the retry, Execute the appropriate set of instructions
  38. .ExecuteAsync(async () => await action());
  39.  
  40. //Return the response message gotten from the http client call.
  41. return response;
  42. }
  43.  
  44. var response = await QueryCurrencyServiceWithRetryPolicy(() => _httpClient.GetAsync(ALL_CURRENCIES_URL));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement