title: Polly policy not working using "AddPolicyHandler"
body: <p>I have an application that makes a request for an authenticated service, where it is necessary to pass the <code>access_token</code>.</p>
  <p>My idea is to use Polly to retry if the <code>access_token</code> is expired.</p>
  <p>I'm using Refit (v5.1.67) and Polly (v7.2.1) in a .NET Core 3.1 application.</p>
  <p>The services are registered as follows:</p>
  <pre><code>services.AddTransient&lt;ExampleDelegatingHandler&gt;();
  
  IAsyncPolicy&lt;HttpResponseMessage&gt; retryPolicy = Policy&lt;HttpResponseMessage&gt;
      .Handle&lt;ApiException&gt;()
      .RetryAsync(1, (response, retryCount) =&gt;
      {
          System.Diagnostics.Debug.WriteLine($&quot;Polly Retry =&gt; Count: {retryCount}&quot;);
      });
  
  services.AddRefitClient&lt;TwitterApi&gt;()
      .ConfigureHttpClient(c =&gt;
      {
          c.BaseAddress = new Uri(&quot;https://api.twitter.com/&quot;);
      })
      .AddHttpMessageHandler&lt;ExampleDelegatingHandler&gt;()
      .AddPolicyHandler((sp, req) =&gt;
      {
          //this policy does not works, because the exception is not catched on 
          //&quot;Microsoft.Extensions.Http.PolicyHttpMessageHandler&quot; (DelegatingHandler)
          return retryPolicy;
      });
  </code></pre>
  <pre><code>public interface TwitterApi
  {
      [Get(&quot;/2/users&quot;)]
      Task&lt;string&gt; GetUsers();
  }
  </code></pre>
  <pre><code>public class ExampleDelegatingHandler : DelegatingHandler
  {
      protected async override Task&lt;HttpResponseMessage&gt; SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
      {
          try
          {
              return await base.SendAsync(request, cancellationToken);
          }
          catch (Exception)
          {
              //Why do not catch the exception?
              throw;
          }
      }
  }
  </code></pre>
  <p>The retry policy is not working!</p>
  <p>Analyzing the problem, I realized that the exception is not being caught inside the HttpClient's <code>DelegatingHandler</code>. Since the <code>AddPolicyHandler</code> statement is generating a <code>DelegatingHandler</code> (<code>PolicyHttpMessageHandler</code>) to execute the policy and the exception is not caught there, the policy never executes. I realized that the problem only occurs in asynchronous scenarios, where the request can be sent. In synchronous scenarios it works (example: timeout).</p>
  <p><strong>Why the exception is not caught inside <code>DelegatingHandler</code>??</strong></p>
  <p>I am attaching an example project simulating a Twitter call.</p>
  <p><a href="https://www.dropbox.com/s/q1797rq1pbjvcls/ConsoleApp2.zip?dl=0" rel="nofollow noreferrer">https://www.dropbox.com/s/q1797rq1pbjvcls/ConsoleApp2.zip?dl=0</a></p>
  <p><strong>External references:</strong></p>
  <p><a href="https://github.com/reactiveui/refit#using-httpclientfactory" rel="nofollow noreferrer">https://github.com/reactiveui/refit#using-httpclientfactory</a></p>
  <p><a href="https://www.hanselman.com/blog/UsingASPNETCore21sHttpClientFactoryWithRefitsRESTLibrary.aspx" rel="nofollow noreferrer">https://www.hanselman.com/blog/UsingASPNETCore21sHttpClientFactoryWithRefitsRESTLibrary.aspx</a></p>
  <p><a href="https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1" rel="nofollow noreferrer">https://docs.microsoft.com/en-us/aspnet/core/fundamentals/http-requests?view=aspnetcore-3.1</a></p>