I just read about Polly library and I need to handle 3 retries when communicating from a desktop agent to a server.
Currently I like the exponential backoff.
However, I struggle to understand how to implement this in my code. This is what I have done so far:
using (HttpClient client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", bearerToken); client.DefaultRequestHeaders.Add("TenantId", tenantId); #if DEBUG var url = "http://localhost:5001/api/v1/RetrievePaymentDetails?orderpaymentid={orderPaymentId"; #else //TODO: add URL once the application is in production! var url = "intent2.api"; #endif Policy .Handle<HttpRequestException>() .WaitAndRetry(new[] { TimeSpan.FromSeconds(5), TimeSpan.FromSeconds(15), TimeSpan.FromSeconds(30) }); using (HttpResponseMessage response = await client.GetAsync($"{url}")) { using (HttpContent content = response.Content) { HttpContentHeaders headers = content.Headers; var result = JsonConvert.DeserializeObject<PaymentDetailsDto>(response.Content.ReadAsStringAsync().Result); Currency currencyFromDb = (Currency)Enum.Parse(typeof(Currency), result.Currency); var result2 = await this.posManager.PayPos(result.Amount, currencyFromDb, result.SumUpUsername, result.SumUpPassword); if (result2.TransactionCode == null) { return this.Request.CreateResponse(HttpStatusCode.BadRequest, result2); } return this.Request.CreateResponse(HttpStatusCode.OK, result2); //return this.Request.CreateResponse<object>(result2); } } } I turned off my web server so I can simulate this exercise. Now, every time it hits that URL it throws a HttpRequestException and does not retry at all. How do I go about using this?
using (HttpClient client = new HttpClient())