{"record":{"id":"095199158cee65a0","repo":"elsa-workflows/elsa-core","slug":"the-specified-activity-is-not-part-of-the-workflow-095199","errorCode":null,"errorMessage":"The specified activity is not part of the workflow.","messagePattern":"The specified activity is not part of the workflow\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Workflows.Core/Services/WorkflowExecutionContextSchedulerStrategy.cs","lineNumber":14,"sourceCode":"using Elsa.Workflows.Models;\nusing Elsa.Workflows.Options;\n\nnamespace Elsa.Workflows;\n\n/// <inheritdoc />\npublic class WorkflowExecutionContextSchedulerStrategy : IWorkflowExecutionContextSchedulerStrategy\n{\n    /// <inheritdoc />\n    public ActivityWorkItem Schedule(WorkflowExecutionContext context, ActivityNode activityNode, ActivityExecutionContext owner, ScheduleWorkOptions? options = null)\n    {\n        // Validate that the specified activity is part of the workflow.\n        if (!context.NodeActivityLookup.ContainsKey(activityNode.Activity))\n            throw new InvalidOperationException(\"The specified activity is not part of the workflow.\");\n\n        var scheduler = context.Scheduler;\n\n        if (options?.PreventDuplicateScheduling == true)\n        {\n            // Check if the activity is already scheduled for the specified owner.\n            var existingWorkItem = scheduler.Find(x => x.Activity.NodeId == activityNode.NodeId && x.Owner == owner);\n\n            if (existingWorkItem != null)\n                return existingWorkItem;\n        }\n\n        var activity = activityNode.Activity;\n        var tag = options?.Tag;\n\n        // Use explicit SchedulingActivityExecutionId from options, or fall back to owner context.\n        var schedulingActivityExecutionId = options?.SchedulingActivityExecutionId ?? owner.Id;\n","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Core/Services/WorkflowExecutionContextSchedulerStrategy.cs#L1-L32","documentation":"Thrown by WorkflowExecutionContextSchedulerStrategy.Schedule when the requested ActivityNode's underlying Activity is not in the workflow execution context's NodeActivityLookup, i.e., the node does not belong to the currently executing workflow graph. This is the main-scheduler counterpart of the background-execution check and guards the scheduler from operating on foreign activities.","triggerScenarios":"Calling context.Schedule / ScheduleActivity with an ActivityNode built from another workflow's graph, a node from a stale graph after re-loading/rebuilding the workflow, or a node constructed manually (new ActivityNode(activity)) for an activity not committed to this context.","commonSituations":"Custom scheduler integrations that cache ActivityNodes across workflow restarts, composite activities scheduling nodes from a different composite instance, and hot-reloaded definitions where old node references linger.","solutions":["Obtain the ActivityNode from the current context graph (e.g., context.FindNodeById / NodeActivityLookup) rather than caching or constructing nodes manually.","Rebuild any cached node references after the workflow definition is reloaded or the instance is resumed.","Ensure composite children are registered in the graph so their nodes exist in this context before scheduling."],"exampleFix":"// before\nvar node = new ActivityNode(myActivity); // foreign to this workflow\ncontext.Scheduler.ScheduleItem(new ActivityWorkItem(node.Id, ...));\n\n// after\nvar node = context.NodeActivityLookup[myActivity] ?? context.FindNodeById(myActivity.Id);\nif (node == null) throw new InvalidOperationException(\"Activity not in current workflow\");\nawait context.ScheduleActivityAsync(myActivity, onCompleted);","handlingStrategy":"validation","validationCode":"if (!context.NodeActivityLookup.ContainsKey(activityNode.Activity))\n    throw new InvalidOperationException(\"ActivityNode does not belong to this workflow context graph.\");","typeGuard":"bool NodeBelongsToContext(WorkflowExecutionContext ctx, ActivityNode n) => ctx.NodeActivityLookup.ContainsKey(n.Activity);","tryCatchPattern":"try\n{\n    var workItem = strategy.Schedule(context, node, owner, options);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not part of the workflow\"))\n{\n    logger.LogWarning(ex, \"Foreign node {NodeId} rejected by scheduler\", node.NodeId);\n}","preventionTips":["Always obtain ActivityNode instances from the live context graph, never from caches or manual construction.","Invalidate cached nodes whenever a workflow definition reloads or an instance resumes.","Use context.ScheduleActivityAsync helpers that resolve nodes internally."],"tags":["workflow","scheduling","validation"],"backgroundTag":"invalid-argument-value","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}