From 2b909997bcbdf5a42cc351f2b29dc29607632c46 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Tue, 10 Oct 2023 12:55:44 +0200 Subject: [PATCH] Fix duplicate key issue for older definition formats --- .../Contexts/WorkflowExecutionContext.cs | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/src/modules/Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.cs b/src/modules/Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.cs index 00365ec97..cda1b2fa5 100644 --- a/src/modules/Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.cs +++ b/src/modules/Elsa.Workflows.Core/Contexts/WorkflowExecutionContext.cs @@ -191,7 +191,10 @@ public class WorkflowExecutionContext : IExecutionContext Nodes = nodes; NodeIdLookup = nodes.ToDictionary(x => x.NodeId); NodeHashLookup = nodes.ToDictionary(x => Hash(x.NodeId)); - NodeActivityIdLookup = nodes.ToDictionary(x => x.Activity.Id); + + // Only new tooling uses unique activity IDs. For older tooling, we will rely on a linear search. + if (workflow.CreatedWithModernTooling()) + NodeActivityIdLookup = nodes.ToDictionary(x => x.Activity.Id); } /// @@ -275,7 +278,7 @@ public class WorkflowExecutionContext : IExecutionContext /// /// A map between s and s in the workflow graph. /// - public IDictionary NodeActivityIdLookup { get; private set; } = default!; + public IDictionary? NodeActivityIdLookup { get; private set; } = default!; /// /// The for the execution context. @@ -444,12 +447,15 @@ public class WorkflowExecutionContext : IExecutionContext /// /// Returns the containing the specified activity from the workflow graph. /// - public ActivityNode? FindNodeByActivity(IActivity activity) => NodeActivityIdLookup[activity.Id]; + public ActivityNode? FindNodeByActivity(IActivity activity) + { + return NodeActivityIdLookup?[activity.Id] ?? Nodes.FirstOrDefault(x => x.Activity == activity); + } /// /// Returns the associated with the specified activity ID. /// - public ActivityNode? FindNodeByActivityId(string activityId) => NodeActivityIdLookup[activityId]; + public ActivityNode? FindNodeByActivityId(string activityId) => NodeActivityIdLookup?[activityId] ?? Nodes.FirstOrDefault(x => x.Activity.Id == activityId); /// /// Returns the with the specified ID from the workflow graph. @@ -554,17 +560,17 @@ public class WorkflowExecutionContext : IExecutionContext /// Adds the specified to the workflow execution context. /// public void AddActivityExecutionContext(ActivityExecutionContext context) => _activityExecutionContexts.Add(context); - + /// /// Removes the specified from the workflow execution context. /// public void RemoveActivityExecutionContext(ActivityExecutionContext context) => _activityExecutionContexts.Remove(context); - + /// /// Removes the specified from the workflow execution context. /// /// The predicate used to filter the activity execution contexts to remove. - public void RemoveActivityExecutionContext(Func predicate) => _activityExecutionContexts.RemoveWhere(predicate); + public void RemoveActivityExecutionContext(Func predicate) => _activityExecutionContexts.RemoveWhere(predicate); /// /// Records the output of the specified activity into the current workflow execution context.