Azure Function slow HTTP requests compared to when running in localhost
- Dominant language
- PowerShell
- Stars
- 1.1k
- Forks
- 215
- Avg merge
- 4h 2m
- Merged PRs (30d)
- 1
Description
I'm using a Azure Function .Net 6 Isolated.
When I run the function on localhost from VS2022, it is up to 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).
The same code running in localhost Vs. Azure

Results in different regions vs. localhost:

This behaves exactly the same in my student subscription.
The code is as follows:
DI - using the IHttpClientFactory (here):
```
public static class DataSourceServiceRegistration
{
public static IServiceCollection RegisterDataSourceServices(this IServiceCollection serviceCollection)
{
serviceCollection.AddHttpClient();
return serviceCollection;
}
}
```
HttpClient usage:
```
private readonly HttpClient _httpClient;
public EsriHttpClientAdapter(HttpClient httpClient)
{
_httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
}
public async Task SendPrintServiceMessage(string url, HttpMethod httpMethod, string referer, IEnumerable> 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("some", "config");
_logger.LogInformation($"Before SendAsync - time {watch.ElapsedMilliseconds}");
var result = await _httpClient.SendAsync(msg);
_logger.LogInformation($"After SendAsync - time {watch.ElapsedMilliseconds}");
var response = await result.Content.ReadAsStringAsync();
_logger.LogInformation($"After ReadAsStringAsync - time {watch.ElapsedMilliseconds}");
if (result.StatusCode == HttpStatusCode.OK)
{
//do some stuff here
}
}
```
Application Insights is as follows:
AZURE hosted:

Localhost:

Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.