'How to wait for async task in a List of KeyValuePairs

I have the following code...

List<Task<KeyValuePair<string, SomeLongWaitingResponse>>> LongWaitedResponses
    = new List<Task<KeyValuePair<string, SomeLongWaitingResponse>>>();

foreach(var request in requests)
{
    async Task<KeyValuePair<string, SomeLongWaitingResponse>> func()
    {
        return new KeyValuePair<string, SomeLongWaitingResponse>(requestId,
            await _service.SomeLongWaitingRequest(request));
    }
    LongWaitedResponses.Add(func());
}

var result = await Task.WhenAll(LongWaitedResponses);

How do I wait for all LongWaitedResponses to finish? I want to wait for all of them and then continue with execution. Problem I encounter is that my WhenAll continues even SomeLongWaitingResponse has not come.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source