{"record":{"id":"e39a170a703bf0f5","repo":"elsa-workflows/elsa-core","slug":"no-parent-variable-container-found","errorCode":null,"errorMessage":"No parent variable container found","messagePattern":"No parent variable container found","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Workflows.Core/Extensions/ActivityExecutionContextExtensions.cs","lineNumber":119,"sourceCode":"        public Variable SetVariable(string name, object? value, Action<MemoryBlock>? configure = null) =>\n            context.ExpressionExecutionContext.SetVariable(name, value, configure);\n\n        public Variable SetDynamicVariable<T>(string name, T value, Action<MemoryBlock>? configure = null)\n        {\n            // Check if a predefined variable already exists.\n            var predefinedVariable = context.ExpressionExecutionContext.GetVariable(name);\n\n            if (predefinedVariable != null)\n            {\n                context.SetVariable(name, value);\n                return predefinedVariable;\n            }\n\n            // No predefined variable exists, so we will add a dynamic variable to the current container in scope.\n            var container = context.FindParentWithVariableContainer();\n\n            if (container == null)\n                throw new(\"No parent variable container found\");\n\n            var existingVariable = container.DynamicVariables.FirstOrDefault(x => x.Name == name);\n\n            if (existingVariable == null)\n                container.DynamicVariables.Add(new Variable<T>(name, value));\n\n            return context.SetVariable(name, value);\n        }\n\n        /// <summary>\n        /// Gets a workflow variable by name.\n        /// </summary>\n        /// <param name=\"name\">The name of the variable.</param>\n        /// <typeparam name=\"T\">The type of the variable.</typeparam>\n        /// <returns>The variable if found, otherwise null.</returns>\n        public T? GetVariable<T>(string name) => context.ExpressionExecutionContext.GetVariable<T?>(name);\n\n        /// <summary>","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Core/Extensions/ActivityExecutionContextExtensions.cs#L101-L137","documentation":"When setting a variable by name with SetVariable, the extension first looks for a predefined variable in scope; if none exists it tries to add a dynamic variable to the nearest ancestor that exposes a variable container. If no parent scope has an IVariableContainer at all, there is nowhere to store the variable and this exception is thrown.","triggerScenarios":"Calling ActivityExecutionContext.SetVariable/GetOrCreateVariable-style APIs from a context whose ancestry contains no variable container — e.g. an activity executed outside any workflow/composite scope that owns variables, or a detached ActivityExecutionContext.","commonSituations":"Setting variables from custom activities running in a root context that lacks a VariableContainer; using variable APIs in background/scheduled work where the execution context is no longer attached to a workflow; misconfigured composites that do not declare variables.","solutions":["Ensure the workflow (root activity) or an enclosing composite/container in the activity's ancestry declares a variable container.","Wrap the scope in a Flowchart/Composite/Sequence (which provide variable containers) where the variable is set.","Create the Variable explicitly and add it to the intended container rather than relying on dynamic scope resolution.","Check that you are not calling the API on a stale or detached ActivityExecutionContext; obtain the context from a live workflow execution."],"exampleFix":"// before\ncontext.SetVariable(\"count\", 42); // throws when no parent container\n// after\nvar container = context.FindParentWithVariableContainer();\nif (container == null)\n    container = new Variable { Name = \"count\", Value = 42 }; // or ensure a Composite/Workflow ancestor declares variables\nelse\n    context.SetVariable(\"count\", 42);","handlingStrategy":"validation","validationCode":"var container = context.FindParentWithVariableContainer();\nif (container == null) throw new InvalidOperationException(\"No variable container in scope; wrap the activity in a workflow/composite that declares variables\");","typeGuard":"bool hasContainer = context.FindParentWithVariableContainer() != null;","tryCatchPattern":"try { context.SetVariable(name, value); } catch (Exception ex) { logger.LogError(ex, \"Cannot set variable {Name}: no variable container in scope\", name); }","preventionTips":["Ensure the root workflow or an enclosing composite declares a variable container","Do not use variable APIs on detached or stale ActivityExecutionContexts","Declare expected variables statically in composites instead of relying on dynamic creation"],"tags":["variables","scope","csharp"],"backgroundTag":"resource-not-found","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}