elsa-workflows / elsa-workflows/elsa-core
Bug Report: Issue with Workflow Level Variables Loading in Elsa 3 Framework
- Dominant language
- C#
- Stars
- 7.9k
- Forks
- 1.5k
- Avg merge
- 15h 22m
- Merged PRs (30d)
- 114
Description
Dear Elsa 3 Framework Support Team,
I am writing to report an issue we have encountered while working with the Elsa 3 Framework. We are experiencing a problem related to the loading of workflow level variables.
## Description of the Issue:
During the implementation of workflows, we observed that the variables designated at the workflow level are not being properly loaded or recognized.
This issue seems to occur sporadically, without a clear pattern or specific triggering condition.
## Impact:
This problem is impacting our ability to reliably execute workflows, as the expected behavior of these variables is essential for the correct functioning of our processes.
It is causing delays and inconsistencies in our workflow execution, which is a critical part of our operations.
## Steps to Reproduce:
Define workflow level variables in the Elsa Dashboard.
Trigger the workflow.
Observe that the variables are not loaded as expected during the workflow execution.
My Scene:

I defined a variable called SessionPayload and this variable is initialized with the command below:
````csharp
var sessionPayload = SessionPayload.Get(context);
context.SetVariable("SessionPayload", sessionPayload);
````
I have an activity that is executed in the middle of the flow called ChatBotTrigger that is executed with the code below, in this activity the line
````csharp
var model = context.GetVariable("SessionPayload");
````
the value is returned successfully.
````csharp
public class ChatBotTrigger : CodeActivity
{
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var model = context.GetVariable("SessionPayload");
if (string.Equals(model?.Type, "IncomingMessage", StringComparison.OrdinalIgnoreCase))
{
await context.CompleteActivityWithOutcomesAsync("IncomingMessage");
}
else if (string.Equals(model?.Type, "RestApi", StringComparison.OrdinalIgnoreCase))
{
await context.CompleteActivityWithOutcomesAsync("RestApi");
}
else
{
await context.CompleteActivityWithOutcomesAsync("Failed");
}
context.SetResult(model);
}
}
````
As shown in the workflow, I have a timer that after 10 seconds calls an activity called ChatBotUnsetSession that contains the following implementation (The same get variable code):
````csharp
public class ChatBotUnsetSession : CodeActivity
{
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var sessionPayload = context.GetVariable("SessionPayload");
var cache = context.GetRequiredService>();
if (!string.IsNullOrEmpty(sessionPayload?.SessionId))
{
await cache.RemoveAsync(sessionPayload.SessionId);
}
}
}
````
when the line
````csharp
var sessionPayload = context.GetVariable("SessionPayload");
````
and executed we have the following return in class ExpressionExecutionContextExtensions:

````csharp
foreach (var block in context.Memory.Blocks.Where(b => b.Value.Metadata is VariableBlockMetadata))
{
var metadata = block.Value.Metadata as VariableBlockMetadata;
if (metadata!.Variable.Name == name)
return metadata.Variable; <-- Here the value is empty and the type is wrong
}
return localScopeOnly ? null : context.ParentContext?.GetVariable(name);
````
how can we see the returned value is empty and the type is also wrong
when we return to the origin function
````csharp
public static T? GetVariable(this ExpressionExecutionContext context, string name) => (T?)context.GetVariable(name)?.Value;
````
we have an empty value and the wrong type.
In my head there was a problem implementing the workflow type storage, looking at the code I didn't find anything problematic, but I still wrote a custom storage to save in Redis with the impl below, but the problem remains the same.
````csharp
public class RedisStorageProvider(IDistributedCache distributedCache) : IStorageDriver
{
public async ValueTask WriteAsync(string id, object value, StorageDriverContext context)
{
await distributedCache.SetAsync(id, value, token: context.CancellationToken);
}
public async ValueTask ReadAsync(string id, StorageDriverContext context)
{
return await distributedCache.GetAsync(id, token: context.CancellationToken);
}
public async ValueTask DeleteAsync(string id, StorageDriverContext context)
{
await distributedCache.RemoveAsync(id, token: context.CancellationToken);
}
}
````
Environment:
Elsa 3 Framework version: 3.0.0/3.0.1
Packages:
````csharp
````
Host environment: Windows, Rider
We kindly request your assistance in resolving this issue. If there are any workarounds or patches available, please let us know. Additionally, if further information is required from our end to diagnose the problem, feel free to reach out.
Thank you for your attention to this matter. We appreciate your prompt response and look forward to a resolution.
Best regards,
Contributor guide
Assessment
This issue has not been assessed yet.