using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Dapr.Workflow; using FanOutFanIn.Activities; namespace FanOutFanIn; internal sealed class FanOutFanInWorkflow : Workflow { public override async Task RunAsync(WorkflowContext context, string[] input) { if (input.Length == 0) { throw new ArgumentException("Input cannot be empty.", nameof(input)); } // This list will contain the tasks that will be executed by the Dapr Workflow engine. List> tasks = new(); foreach (string item in input) { // Tasks are added to the list tasks.Add(context.CallActivityAsync( nameof(GetWordLength), item)); } // The Dapr Workflow engine will schedule all the tasks and wait for all tasks to complete before continuing. var allWordLengths = await Task.WhenAll(tasks); var shortestWord = allWordLengths.OrderBy(wl => wl.Length).First(); return shortestWord.Word; } }