elsa-workflows/elsa-core · error · InvalidOperationException

The specified activity is not part of the workflow.

Error message

The specified activity is not part of the workflow.

What it means

ActivityExecutionContextSchedulerStrategy.ScheduleActivityAsync throws InvalidOperationException when the activity instance to schedule cannot be found in the current workflow's node graph via WorkflowExecutionContext.FindNodeByActivity. You can only schedule activities that are structurally part of the executing workflow. This usually means the activity belongs to a different workflow instance or was not part of the built graph.

Solutions

  1. Schedule activities that are part of the workflow — reference the declared activity field/property instead of constructing a new instance
  2. If using the node overload, fetch the node via context.WorkflowExecutionContext.FindNodeByActivity first and handle null before scheduling
  3. Register dynamically created activities in the workflow graph (Composite/ActivitySchedulerNode plumbing) before scheduling them
  4. Verify the activity belongs to the currently executing workflow instance, not another definition or a stale instance

Example fix

// before
await context.ScheduleActivityAsync(new WriteLine(context.Get("msg")) as IActivity); // not part of the graph
// after
var target = context.WorkflowExecutionContext.FindNodeByActivity(MyDeclaredActivity);
if (target != null)
    await context.ScheduleActivityAsync(MyDeclaredActivity);
Defensive patterns

Strategy: validation

Validate before calling

var node = context.WorkflowExecutionContext.FindNodeByActivity(activity);
if (node == null)
    throw new InvalidOperationException("Activity is not part of this workflow; cannot schedule.");
await context.ScheduleActivityAsync(activity);

Type guard

static bool IsPartOfWorkflow(WorkflowExecutionContext ctx, IActivity a) => ctx.FindNodeByActivity(a) != null;

Try / catch

try { await context.ScheduleActivityAsync(activity); }
catch (InvalidOperationException ex) when (ex.Message.Contains("not part of the workflow")) { logger.LogWarning("Attempted to schedule foreign activity {Activity}", activity.Type); }

Prevention

When it happens

Trigger: Calling context.ScheduleActivity(activity) with an activity object created outside the workflow (new'ed up at runtime), an activity from another workflow definition/instance, or a dynamically composed activity not registered in the node hierarchy; also when an activity was added after workflow graph construction.

Common situations: Scheduling activities from within a custom activity by constructing 'new SomeActivity()' instead of referencing one declared in the workflow; holding stale activity references from a previous instance after workflow reload; custom workflow builders that bypass graph registration.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


AI-assisted analysis of elsa-workflows/elsa-core@fe9217bdfa (2026-09-13). Data as JSON: /api/errors/88a0af8b398e67e9. Report an issue: GitHub.

Appendix: source

Thrown at src/modules/Elsa.Workflows.Core/Services/ActivityExecutionContextSchedulerStrategy.cs:14

using Elsa.Extensions;
using Elsa.Workflows.Models;
using Elsa.Workflows.Options;

namespace Elsa.Workflows;

/// <inheritdoc />
public class ActivityExecutionContextSchedulerStrategy : IActivityExecutionContextSchedulerStrategy
{
    /// <inheritdoc />
    public async Task ScheduleActivityAsync(ActivityExecutionContext context, IActivity? activity, ActivityExecutionContext? owner, ScheduleWorkOptions? options = null)
    {
        var activityNode = activity != null
            ? context.WorkflowExecutionContext.FindNodeByActivity(activity) ?? throw new InvalidOperationException("The specified activity is not part of the workflow.")
            : null;
        await ScheduleActivityAsync(context, activityNode, owner, options);
    }

    /// <inheritdoc />
    public async Task ScheduleActivityAsync(ActivityExecutionContext context, ActivityNode? activityNode, ActivityExecutionContext? owner = null, ScheduleWorkOptions? options = null)
    {
        var workflowExecutionContext = context.WorkflowExecutionContext;
        if (context.GetIsBackgroundExecution())
        {
            // Validate that the specified activity is part of the workflow.
            if (activityNode != null && !workflowExecutionContext.NodeActivityLookup.ContainsKey(activityNode.Activity))
                throw new InvalidOperationException("The specified activity is not part of the workflow.");

            var scheduledActivity = new ScheduledActivity
            {
                ActivityNodeId = activityNode?.NodeId,
                OwnerActivityInstanceId = owner?.Id,

View on GitHub (pinned to fe9217bdfa)