{"record":{"id":"88a0af8b398e67e9","repo":"elsa-workflows/elsa-core","slug":"the-specified-activity-is-not-part-of-the-workflow","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/ActivityExecutionContextSchedulerStrategy.cs","lineNumber":14,"sourceCode":"using Elsa.Extensions;\nusing Elsa.Workflows.Models;\nusing Elsa.Workflows.Options;\n\nnamespace Elsa.Workflows;\n\n/// <inheritdoc />\npublic class ActivityExecutionContextSchedulerStrategy : IActivityExecutionContextSchedulerStrategy\n{\n    /// <inheritdoc />\n    public async Task ScheduleActivityAsync(ActivityExecutionContext context, IActivity? activity, ActivityExecutionContext? owner, ScheduleWorkOptions? options = null)\n    {\n        var activityNode = activity != null\n            ? context.WorkflowExecutionContext.FindNodeByActivity(activity) ?? throw new InvalidOperationException(\"The specified activity is not part of the workflow.\")\n            : null;\n        await ScheduleActivityAsync(context, activityNode, owner, options);\n    }\n\n    /// <inheritdoc />\n    public async Task ScheduleActivityAsync(ActivityExecutionContext context, ActivityNode? activityNode, ActivityExecutionContext? owner = null, ScheduleWorkOptions? options = null)\n    {\n        var workflowExecutionContext = context.WorkflowExecutionContext;\n        if (context.GetIsBackgroundExecution())\n        {\n            // Validate that the specified activity is part of the workflow.\n            if (activityNode != null && !workflowExecutionContext.NodeActivityLookup.ContainsKey(activityNode.Activity))\n                throw new InvalidOperationException(\"The specified activity is not part of the workflow.\");\n\n            var scheduledActivity = new ScheduledActivity\n            {\n                ActivityNodeId = activityNode?.NodeId,\n                OwnerActivityInstanceId = owner?.Id,","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Core/Services/ActivityExecutionContextSchedulerStrategy.cs#L1-L32","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Schedule activities that are part of the workflow — reference the declared activity field/property instead of constructing a new instance","If using the node overload, fetch the node via context.WorkflowExecutionContext.FindNodeByActivity first and handle null before scheduling","Register dynamically created activities in the workflow graph (Composite/ActivitySchedulerNode plumbing) before scheduling them","Verify the activity belongs to the currently executing workflow instance, not another definition or a stale instance"],"exampleFix":"// before\nawait context.ScheduleActivityAsync(new WriteLine(context.Get(\"msg\")) as IActivity); // not part of the graph\n// after\nvar target = context.WorkflowExecutionContext.FindNodeByActivity(MyDeclaredActivity);\nif (target != null)\n    await context.ScheduleActivityAsync(MyDeclaredActivity);","handlingStrategy":"validation","validationCode":"var node = context.WorkflowExecutionContext.FindNodeByActivity(activity);\nif (node == null)\n    throw new InvalidOperationException(\"Activity is not part of this workflow; cannot schedule.\");\nawait context.ScheduleActivityAsync(activity);","typeGuard":"static bool IsPartOfWorkflow(WorkflowExecutionContext ctx, IActivity a) => ctx.FindNodeByActivity(a) != null;","tryCatchPattern":"try { await context.ScheduleActivityAsync(activity); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not part of the workflow\")) { logger.LogWarning(\"Attempted to schedule foreign activity {Activity}\", activity.Type); }","preventionTips":["Only schedule activities declared as part of the workflow, never freshly constructed instances","Cache activity references from the current instance, not previous instances","For dynamic activities, register them in the workflow graph before scheduling"],"tags":["workflow","scheduling","invalid-argument","runtime"],"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-15T23:17:13.987Z"}