{"record":{"id":"95e05625e1e71fe3","repo":"elsa-workflows/elsa-core","slug":"failed-to-retrieve-memory-block-with-reference","errorCode":null,"errorMessage":"Failed to retrieve memory block with reference {blockReference.Id}","messagePattern":"Failed to retrieve memory block with reference (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Expressions/Models/ExpressionExecutionContext.cs","lineNumber":50,"sourceCode":"    /// <summary>\n    /// Provides access to the parent <see cref=\"ExpressionExecutionContext\"/>, if there is any.\n    /// </summary>\n    public ExpressionExecutionContext? ParentContext { get; set; } = parentContext;\n\n    /// <summary>\n    /// A cancellation token.\n    /// </summary>\n    public CancellationToken CancellationToken { get; } = cancellationToken;\n\n    /// <summary>\n    /// Returns the <see cref=\"MemoryBlock\"/> pointed to by the specified memory block reference.\n    /// </summary>\n    public MemoryBlock GetBlock(Func<MemoryBlockReference> blockReference) => GetBlock(blockReference());\n\n    /// <summary>\n    /// Returns the <see cref=\"MemoryBlock\"/> pointed to by the specified memory block reference.\n    /// </summary>\n    public MemoryBlock GetBlock(MemoryBlockReference blockReference) => GetBlockInternal(blockReference) ?? throw new Exception($\"Failed to retrieve memory block with reference {blockReference.Id}\");\n\n    /// <summary>\n    /// Returns the <see cref=\"MemoryBlock\"/> pointed to by the specified memory block reference.\n    /// </summary>\n    public bool TryGetBlock(MemoryBlockReference blockReference, out MemoryBlock block)\n    {\n        var b = GetBlockInternal(blockReference);\n        block = b ?? null!;\n        return b != null;\n    }\n\n    /// <summary>\n    /// Returns the value of the memory block pointed to by the specified memory block reference.\n    /// </summary>\n    public object? Get(Func<MemoryBlockReference> blockReference) => Get(blockReference());\n\n    /// <summary>\n    /// Returns the value of the memory block pointed to by the specified memory block reference.","sourceCodeStart":32,"sourceCodeEnd":68,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Expressions/Models/ExpressionExecutionContext.cs#L32-L68","documentation":"ExpressionExecutionContext.GetBlock looks up the MemoryBlock that a MemoryBlockReference points to in the context's memory register. When the reference's Id does not resolve to an existing block, the internal lookup returns null and this generic Exception is thrown. It means a variable/argument reference was used but no storage slot was ever allocated for it in this execution scope or its parents.","triggerScenarios":"Calling ExpressionExecutionContext.GetBlock(Func<MemoryBlockReference>) or GetBlock(MemoryBlockReference) with a reference whose Id was never registered via memory allocation (e.g. a Variable that was not declared, or a reference created against a different context/owner).","commonSituations":"Referencing a variable from a workflow fragment executed in a context where the variable was never set; custom activities reading variables with GetBlock instead of Get<T>; expression code capturing a Variable object whose block was removed or never allocated in the current scope chain.","solutions":["Use TryGetBlock(ref, out var block) instead of GetBlock to handle missing blocks gracefully.","Ensure the variable/argument is declared and set (ExecuteAsync with the variable or a SetVariable/Set value activity) before reading it.","Verify the memory reference is obtained from the correct ExpressionExecutionContext / owner so its Id matches an allocated block."],"exampleFix":"// before\nvar block = context.GetBlock(() => myVariable.GetReferenceTo(context));\n// after\nif (context.TryGetBlock(() => myVariable.GetReferenceTo(context), out var block))\n{\n    // use block\n}","handlingStrategy":"type-guard","validationCode":"if (context.TryGetBlock(() => variable.GetReferenceTo(context), out var block)) { /* proceed */ } else { /* handle missing */ }","typeGuard":"static bool HasBlock(ExpressionExecutionContext ctx, MemoryBlockReference r) => ctx.TryGetBlock(r, out _);","tryCatchPattern":"try { var block = context.GetBlock(reference); } catch (Exception ex) when (ex.Message.StartsWith(\"Failed to retrieve memory block\")) { /* allocate or fail gracefully */ }","preventionTips":["Prefer TryGetBlock over GetBlock when the variable may be undeclared.","Use the typed Get<T>/Set extensions on Variable rather than raw memory references.","Declare all variables at the workflow/composite root before custom activity execution."],"tags":["csharp","memory","workflow-variables"],"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-15T23:17:13.987Z"}