UnhandledException when using HttpClient with Polly for transient fault handling in .NET 6
I can't seem to get Quick question that's been bugging me - I'm stuck on something that should probably be simple. I'm attempting to implement a retry policy using Polly with `HttpClient` to handle transient faults in my .NET 6 application. However, I'm encountering an `UnhandledException` when the retry limit is exceeded. Hereβs the code snippet I'm working with: ```csharp using System; using System.Net.Http; using System.Threading.Tasks; using Polly; using Polly.Extensions.Http; public class ApiService { private readonly HttpClient _httpClient; private readonly IAsyncPolicy<HttpResponseMessage> _retryPolicy; public ApiService(HttpClient httpClient) { _httpClient = httpClient; _retryPolicy = HttpPolicyExtensions .Handle<HttpRequestException>() .Or<TaskCanceledException>() .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt))); } public async Task<string> GetDataAsync(string url) { var response = await _retryPolicy.ExecuteAsync(() => _httpClient.GetAsync(url)); response.EnsureSuccessStatusCode(); return await response.Content.ReadAsStringAsync(); } } ``` I'm making a call to `GetDataAsync` from another method, and if the server is down or if there's a timeout, I expect Polly to retry the request. However, after the third retry, I'm seeing the following error: ``` Unhandled exception. System.Net.Http.HttpRequestException: The request was canceled due to the configured HttpClient.Timeout of 100 seconds elapsing. ---> System.TimeoutException: The operation has timed out. ``` I have already verified that the `HttpClient` timeout is set appropriately in my `Startup.cs`: ```csharp services.AddHttpClient<ApiService>(client => { client.Timeout = TimeSpan.FromSeconds(100); }); ``` But it appears that Polly is not catching the timeout exception as expected. Iβve also tried logging the exception within the retry policy to gather more context, but it still bubbles up after the retries are exhausted. What could I be missing here? Is there a specific way Polly handles `TimeoutException` that differs from other exceptions? Any insights would be greatly appreciated. For context: I'm using C# on Ubuntu. What's the best practice here? Has anyone dealt with something similar?