diff --git a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs index b09dec91f4..03131daf8c 100644 --- a/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs +++ b/dotnet/src/Microsoft.Agents.AI.Workflows/WorkflowBuilderExtensions.cs @@ -2,7 +2,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Microsoft.Shared.Diagnostics; namespace Microsoft.Agents.AI.Workflows; @@ -58,10 +57,9 @@ public static class WorkflowBuilderExtensions { Throw.IfNull(builder); Throw.IfNull(source); - Throw.IfNull(targets); Func predicate = WorkflowBuilder.CreateConditionFunc(IsAllowedTypeAndMatchingCondition)!; - List targetList = targets.Select(target => Throw.IfNull(target, nameof(targets))).ToList(); + List targetList = ValidateTargets(targets); if (targetList.Count == 1) { @@ -102,10 +100,9 @@ public static class WorkflowBuilderExtensions { Throw.IfNull(builder); Throw.IfNull(source); - Throw.IfNull(targets); Func predicate = WorkflowBuilder.CreateConditionFunc((Func)IsAllowedType)!; - List targetList = targets.Select(target => Throw.IfNull(target, nameof(targets))).ToList(); + List targetList = ValidateTargets(targets); if (targetList.Count == 1) { @@ -119,6 +116,25 @@ public static class WorkflowBuilderExtensions static bool IsAllowedType(object? message) => message is null; } + private static List ValidateTargets(IEnumerable targets) + { + Throw.IfNull(targets); + + List targetList = []; + foreach (ExecutorBinding? target in targets) + { + if (target is null) + { + throw new ArgumentNullException(nameof(targets), "Targets collection cannot contain null elements."); + } + + targetList.Add(target); + } + + Throw.IfNullOrEmpty(targetList, nameof(targets)); + return targetList; + } + /// /// Adds a sequential chain of executors to the workflow, connecting each executor in order so that each is /// executed after the previous one.