title: Using Polly to retry after HttpStatusCode.Unauthorized
body: <p>I'm making calls to an external API and want to deal with the event that a call returns an <code>Unauthorized</code> <code>HttpResponseMessage</code>. When this happens I want to refresh the access token and make the call again.</p>
  
  <p>I'm trying to use <code>Polly</code> with the following code:</p>
  
  <pre><code>    public async Task&lt;HttpResponseMessage&gt; MakeGetRequestAsync()
      {
          var retryPolicy = Policy
              .HandleResult&lt;HttpResponseMessage&gt;(r =&gt; r.StatusCode == HttpStatusCode.Unauthorized)
              .Retry(1, (exception, retryCount) =&gt;
              {
                  RefreshAccessToken();
              });
  
          var result = await retryPolicy.ExecuteAsync(() =&gt; CallApiAsync());
  
          return result;
      }
  
  
      private async Task&lt;HttpResponseMessage&gt; CallApiAsync()
      {
          var url = Options.ResourceSandboxUrl;
  
          var httpClient = new HttpClient();
          SetRequestHeaders(httpClient);
          var response = await httpClient.GetAsync(url);
  
          response.StatusCode = HttpStatusCode.Unauthorized;
          return response;
      }
  </code></pre>
  
  <blockquote>
    <p>I put breakpoints on the <code>ExecuteAsync</code> statement and in <code>DoSomethingAsync</code> - when I step over <code>ExecuteAsync</code> <code>DoSomethingAsync</code> is not called and control is returned to the function that called <code>MakeGetRequestAsync</code></p>
  </blockquote>
  
  <p>I don't understand why <code>DoSomethingAsync</code> is not called - can anyone help me with what I'm trying to achieve?</p>
  
  <p>I've looked at the Polly documentation &amp; Polly questions on Stack Overflow but I can't figure out what's going on..</p>