Running parallel Web Service Calls using await Task.WhenAll(tasks) returning 202 for some requests

Here is my test method

    public async Task<string> Test(int counter)
    {
        var token = "xxxx";
        var requestData = new HttpRequestMessage(HttpMethod.Get, "URL"); 
        requestData.Headers.TryAddWithoutValidation("Authorization", String.Format("Bearer {0}", token));
        var client = _clientFactory.CreateClient();
        var results = await client.SendAsync(requestData);
        var json = await results.Content.ReadAsStringAsync();
        return $"Task {counter} {results.StatusCode};";

    }

Here I am taking a counter as an input to track the task number and returning the statusCode associated with task number.

_clientFactory

is coming from the Dependency Injection like this In Startup.cs

services.AddHttpClient();

In my class

    IHttpClientFactory _clientFactory;
    public TestController(IHttpClientFactory clientFactory, IHttpContextAccessor httpContextAccessor)
    {
        _clientFactory = clientFactory;
        _httpContextAccessor = httpContextAccessor;
    }

Now I am calling the Test method x times like this

            List<Task<string>> tasks = new List<Task<string>>();
            int i = 0;
            while (i < 40)
            {
                var task = Test(i);
                tasks.Add(task);
                i++;
            }
            await Task.WhenAll(tasks);
            var ret = String.Empty;
            foreach (var task in tasks)
            {

                ret += task.Result;
            }

            return Ok(ret);

I am running the Test method in parallel 40 times and concatenating the returns of all the tasks. This runs fine, meaning the status of all the tasks are 200 Ok when I run locally from visual studio which is the expected result. The problem is when I deploy on the server hosted on IIS, I get 200 OK for 50% of the tasks and for other 50% I am getting 202 Accepted. The issue is intermittent and seems to get worst when the parallel number of times get bigger. For 10 parallel calls, it runs successfully in server all the time. I have ran the same api in a different tool JMeter with 40 parallel threads and every request is 200 ok which leads me to believe its not the called api which is sending 202. Am I doing something wrong to not get 200 OK everytime? Is this a normal behaviour of await Task.Whenall? I am using DotNet Core 2.2 as the SDK. The hosting server is Windows server 2016 and IIS version 10. Here is my full controller

 public class TestsController : ControllerBase
{
    private readonly IUnitOfWork<ResponseModel> _uow;
    private IConfiguration _configuration;

    IHttpClientFactory _clientFactory;
    IHttpContextAccessor _httpContextAccessor;



    public TestsController(IUnitOfWork<ResponseModel> uow, IConfiguration configuration, IHttpClientFactory clientFactory, IHttpContextAccessor httpContextAccessor)
    {
        _uow = uow;
        _clientFactory = clientFactory;
        _httpContextAccessor = httpContextAccessor;
        _configuration = configuration;
    }

    [HttpGet()]
    [TypeFilter(typeof(TrackPerformanceFilter))]
    public async Task<IActionResult> Get()
    {
        //List<Task<object>> tasks = new List<Task<object>>();
        List<Task<string>> tasks = new List<Task<string>>();
        int i = 0;
        while (i < 40)
        {
            var task = Test(i);
            tasks.Add(task);
            i++;
        }
        await Task.WhenAll(tasks);
        var ret = String.Empty;
        foreach (var task in tasks)
        {

            ret += task.Result;
        }

        return Ok(ret);

    }
    public async Task<string> Test(int counter)
    {
       
        var token = "";
        var requestData = new HttpRequestMessage(HttpMethod.Get, "");
        if (!string.IsNullOrWhiteSpace(token))
        {
            requestData.Headers.TryAddWithoutValidation("Authorization", String.Format("Bearer {0}", token));
        }
        var client = _clientFactory.CreateClient();
        var results = await client.SendAsync(requestData);
        var json = await results.Content.ReadAsStringAsync();
        return $"Task {counter} {results.StatusCode};";

    }

}
How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum