On a pipeline in my app there is an option to inject a collection of pre-request handlers, that can mutate the request as it first enters the pipeline. The pipeline is fully async, and therefore these pre-request handler calls (as well as everything else) must be awaited. I have a few different ways to call these handlers, and I wondered if there is any difference between them, and if so what would the difference be? For example, would the various handlers be called in the same order? Which option might offer the best performance?
Option 1: foreach
foreach (var handler in this.preRequestHandlers)
{
await handler.Handle(request);
}
Option 2: ForEach()
this.preRequestHandlers.ForEach(async handler => await handler.Handle(request));
Option 3: Task.WhenAll()
await Task.WhenAll(this.preRequestHandlers.Select(handler => handler.Handle(request)));