title: Azure Function slow HTTP requests compared to when running in localhost
body: <p>I'm using a Azure Function .Net 6 Isolated.</p>
  <p>When I run the function on localhost from VS2022, it is 5 times faster then when I deploy it to Azure Function. Localhost is a VM hosted in Azure in the same region as the function.
  I tried different Service Plans, but issue remains. (Consumption Plan, Elastic Premium EP3, Premium V2 P3v2)</p>
  <p><a href="https://i.stack.imgur.com/LLoXd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/LLoXd.png" alt="The same code running in localhost Vs. Azure" /></a></p>
  <p>Results in different regions vs. localhost:
  <a href="https://i.stack.imgur.com/rPRM1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rPRM1.png" alt="enter image description here" /></a></p>
  <p>The code is as follows:</p>
  <p>DI - using the IHttpClientFactory (<a href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests" rel="nofollow noreferrer">here</a>):</p>
  <pre><code>public static class DataSourceServiceRegistration
  {        
     public static IServiceCollection RegisterDataSourceServices(this IServiceCollection serviceCollection)
     {
         serviceCollection.AddHttpClient();
         return serviceCollection;
     }
  }
  </code></pre>
  <p>HttpClient usage:</p>
  <pre><code>private readonly HttpClient _httpClient;
  
  public EsriHttpClientAdapter(HttpClient httpClient)
  {
      _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
  }
  
  public async Task&lt;JsonDocument&gt; SendPrintServiceMessage(string url, HttpMethod httpMethod, string referer, IEnumerable&lt;KeyValuePair&lt;string, string&gt;&gt; content = null)
  {
      var watch = System.Diagnostics.Stopwatch.StartNew();
      HttpContent httpContent = null;
      if (content != null)
      {
          httpContent = new FormUrlEncodedContent(content);
      }
      var msg = new HttpRequestMessage(httpMethod, url) { Content = httpContent };
      _httpClient.DefaultRequestHeaders.Referrer = new Uri(referer);
      _httpClient.DefaultRequestHeaders.Add(&quot;some&quot;, &quot;config&quot;);
  
      _logger.LogInformation($&quot;Before SendAsync - time {watch.ElapsedMilliseconds}&quot;);
      var result = await _httpClient.SendAsync(msg);
      _logger.LogInformation($&quot;After SendAsync - time {watch.ElapsedMilliseconds}&quot;);
      var response = await result.Content.ReadAsStringAsync();
      _logger.LogInformation($&quot;After ReadAsStringAsync - time {watch.ElapsedMilliseconds}&quot;);
  
      if (result.StatusCode == HttpStatusCode.OK)
      {
          //do some stuff here
      }
  }
  </code></pre>
  <p>Application Insights is as follows:</p>
  <p>AZURE:</p>
  <p><a href="https://i.stack.imgur.com/0dTc8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0dTc8.png" alt="enter image description here" /></a></p>
  <p>Localhost:</p>
  <p><a href="https://i.stack.imgur.com/hwepS.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hwepS.png" alt="enter image description here" /></a></p>