Reputation: 11
I have azure function that only have 2 lines of code, first is await Task.Delay(5000) and second is returning status Ok to client. In host.json maxOutstandingRequests and maxConcurrentRequests are set. AF is executed locally and also i tried with deployed version. The problem occurs when I try to send multiple requests from the same HttpClient, function calls will not be async. Execution time per call will be 5, 10, 15, 20 and 25s. When i run same client's code with WebApi instead of AF (WebApi controller has same function as AF), then execution time per each call is 5s. I want to know how can i get the same behavior with Azure function as I have it with WebApi?
Below I provided client's code.
class Program
{
private static System.Net.Http.HttpClient httpClient = new System.Net.Http.HttpClient();
static void Main(string[] args)
{
IEnumerable<TimeSpan> result = Test(10);
Console.WriteLine(string.Join("\n", result));
Console.ReadLine();
}
private static IEnumerable<TimeSpan> Test(int taskCount)
{
Task<TimeSpan>[] tasks = new Task<TimeSpan>[taskCount];
for (int i = 0; i < taskCount; i++)
tasks[i] = Send();
Task.WaitAll(tasks);
return tasks.Select(t => t.Result);
}
private static async Task<TimeSpan> Send()
{
using (var request = new HttpRequestMessage(HttpMethod.Post, ConfigurationManager.AppSettings["AF"]))
{
Stopwatch sw = new Stopwatch();
sw.Start();
using (var response = await httpClient.SendAsync(request))
{
sw.Stop();
return sw.Elapsed;
}
}
}
}
Upvotes: 1
Views: 194
Reputation: 15744
I did some research for this issue. First, I added a line of code in your client's code "Console.WriteLine("--" + sw.Elapsed);".
Then run this code and I saw the 10 Stopwatch start time are printed at the same time.
So this is the reason for the result of 5s, 10s, 15s, 20s and 25s.
After that, if we want to test the async task of azure function, we must add the property in host.json(I think you have completed it)
And then I run the client's code in visual studio and got the result as below screenshot.
I'm not sure if you have the same problem. If you met the same problem as the screenshot above shows. You can install Fiddler4(because according to some research, it may caused by proxy). So I installed Fiddler4 and then run the client's code in visual studio again. It shows the code send 10 requests successfully and the sw.Elapsed time returned are what we expected.
The 10 lines of 6.xx seconds prove the azure function run task async.
Upvotes: 1