Fix duplicate key issue for older definition formats

This commit is contained in:
Sipke Schoorstra 2023-10-10 12:55:44 +02:00
parent ca5692e3bc
commit 2b909997bc

View file

@ -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);
}
/// <summary>
@ -275,7 +278,7 @@ public class WorkflowExecutionContext : IExecutionContext
/// <summary>
/// A map between <see cref="IActivity"/>s and <see cref="ActivityNode"/>s in the workflow graph.
/// </summary>
public IDictionary<string, ActivityNode> NodeActivityIdLookup { get; private set; } = default!;
public IDictionary<string, ActivityNode>? NodeActivityIdLookup { get; private set; } = default!;
/// <summary>
/// The <see cref="IActivityScheduler"/> for the execution context.
@ -444,12 +447,15 @@ public class WorkflowExecutionContext : IExecutionContext
/// <summary>
/// Returns the <see cref="ActivityNode"/> containing the specified activity from the workflow graph.
/// </summary>
public ActivityNode? FindNodeByActivity(IActivity activity) => NodeActivityIdLookup[activity.Id];
public ActivityNode? FindNodeByActivity(IActivity activity)
{
return NodeActivityIdLookup?[activity.Id] ?? Nodes.FirstOrDefault(x => x.Activity == activity);
}
/// <summary>
/// Returns the <see cref="ActivityNode"/> associated with the specified activity ID.
/// </summary>
public ActivityNode? FindNodeByActivityId(string activityId) => NodeActivityIdLookup[activityId];
public ActivityNode? FindNodeByActivityId(string activityId) => NodeActivityIdLookup?[activityId] ?? Nodes.FirstOrDefault(x => x.Activity.Id == activityId);
/// <summary>
/// Returns the <see cref="IActivity"/> with the specified ID from the workflow graph.
@ -554,17 +560,17 @@ public class WorkflowExecutionContext : IExecutionContext
/// Adds the specified <see cref="ActivityExecutionContext"/> to the workflow execution context.
/// </summary>
public void AddActivityExecutionContext(ActivityExecutionContext context) => _activityExecutionContexts.Add(context);
/// <summary>
/// Removes the specified <see cref="ActivityExecutionContext"/> from the workflow execution context.
/// </summary>
public void RemoveActivityExecutionContext(ActivityExecutionContext context) => _activityExecutionContexts.Remove(context);
/// <summary>
/// Removes the specified <see cref="ActivityExecutionContext"/> from the workflow execution context.
/// </summary>
/// <param name="predicate">The predicate used to filter the activity execution contexts to remove.</param>
public void RemoveActivityExecutionContext(Func<ActivityExecutionContext, bool> predicate) => _activityExecutionContexts.RemoveWhere(predicate);
public void RemoveActivityExecutionContext(Func<ActivityExecutionContext, bool> predicate) => _activityExecutionContexts.RemoveWhere(predicate);
/// <summary>
/// Records the output of the specified activity into the current workflow execution context.