elsa-workflows / elsa-core

A .NET workflows library
https://v3.elsaworkflows.io/
MIT License
6.25k stars 1.14k forks source link

[BUG] Null Variables and Non-evaluating C#/JavaScript Expressions in Elsa Workflow Loaded from JSON #5960

Open nitish-tt opened 5 days ago

nitish-tt commented 5 days ago

I am running a WinForms application and loading a workflow from a JSON file exported from Elsa Studio. The workflow runs successfully, but I am unable to use any workflow variables; they always return null. Additionally, any C# or JavaScript expressions I attempt to use do not evaluate, causing a runtime error.

The call stack reveals a NullReferenceException as shown below:

`Elsa.Workflows.Middleware.Activities.ExceptionHandlingMiddleware: Warning: An exception was caught from a downstream middleware component

System.NullReferenceException: Object reference not set to an instance of an object. at Submission#4.<>d__0.MoveNext() --- End of stack trace from previous location --- at Microsoft.CodeAnalysis.Scripting.ScriptExecutionState.RunSubmissionsAsync(...) at Elsa.CSharp.Services.CSharpEvaluator.EvaluateAsync(...) at Elsa.CSharp.Expressions.CSharpExpressionHandler.EvaluateAsync(...) at Elsa.Extensions.ActivityExecutionContextExtensions.EvaluateInputPropertiesAsync(...) at Elsa.Workflows.Middleware.Activities.DefaultActivityInvokerMiddleware.InvokeAsync(...) `

Steps to Reproduce

  1. Load a workflow exported from Elsa Studio in a WinForms application.
  2. Attempt to use workflow variables or evaluate C#/JavaScript expressions within the workflow.
  3. Observe that the workflow completes but variables are null and expressions do not evaluate.

Expected Behavior Variables should be accessible and return valid values, and C# or JavaScript expressions should evaluate correctly during the workflow execution.

Actual Behavior Variables always return null and expressions are not evaluated, leading to a NullReferenceException in the workflow execution.

Log Output Logs show the following warning message related to the null reference error:

`Elsa.Workflows.Middleware.Activities.ExceptionHandlingMiddleware: Warning: An exception was caught from a downstream middleware component

System.NullReferenceException: Object reference not set to an instance of an object. `

Additional Context This issue only occurs when running the workflow from a JSON file exported from Elsa Studio in Winfrom Application. The same expressions and variable manipulations work correctly when run on elsa server.

https://v3.elsaworkflows.io/docs/guides/loading-workflows-from-json this is Code i m using in my Winfrom Aplication.

nitish-tt commented 5 days ago

`using Elsa.Workflows.Attributes; using Elsa.Workflows.Models; using Elsa.Workflows; using System.Text.Json; using Elsa.Extensions;

namespace ElsaServer.StorageActivity { [Activity("StorageActivity", Category = "Data Storage", Description = "Loads a serialized workflow variable's data from a file." )] public class GetVariableValueFromFile : CodeActivity { public Input VariableName { get; set; } = default!;

    public Input<string> FilePath { get; set; } = default!;

    protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
    {
        var filePath = context.Get(FilePath);
        var variableName = context.Get(VariableName);
        var variable = context.WorkflowExecutionContext.Variables.FirstOrDefault(f => f.Name == variableName);
        // this geting null
    }
}

} `

nitish-tt commented 5 days ago

Forgot to mention that I am running Elsa 3.2.0