Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to access scoped variables #1209

Closed
sfmskywalker opened this issue Jul 3, 2021 · 0 comments
Closed

Unable to access scoped variables #1209

sfmskywalker opened this issue Jul 3, 2021 · 0 comments
Assignees
Labels
bug Something isn't working prio high Is on the roadmap for the near-future
Projects
Milestone

Comments

@sfmskywalker
Copy link
Member

sfmskywalker commented Jul 3, 2021

Originally posted by @dodyg in #760 (comment)

Why does in this workflow "CurrentValue" variable is missing from IfTrue

public class Loop : IWorkflow
{
    public void Build(IWorkflowBuilder builder) =>
        builder
        .ForEach(x => new int[] { 10, 20, 30, 40 }, x =>
        {
            x.IfTrue(condition: (ActivityExecutionContext m) =>
            {
                var has = m.HasVariable("CurrentValue");
                Console.WriteLine("Has variable " + has);
                var value = m.GetVariable<int>("CurrentValue");
                Console.WriteLine("Debug Value " + value);
                return value == 40;
            }, whenTrue: (y) =>
            {
                y.WriteLine("The last value");
            });
        });
}

but if I implement a custom activity, the variable are accessible

using Elsa;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Elsa.Builders;
using Elsa.Services;
using Elsa.ActivityResults;
using Elsa.Services.Models;
using System;
using Elsa.Activities.ControlFlow;
using Elsa.Activities.Console;
using Elsa.Activities.Primitives;

using var host = new HostBuilder()
    .ConfigureServices(services =>
    {
        services.AddElsa(elsa =>
        {
            elsa
                .AddConsoleActivities()
                .AddQuartzTemporalActivities()
                .AddWorkflow<Loop>()
                .AddActivity<Print>();
        });
    })
    .UseConsoleLifetime()
    .Build();

await host.Services.GetService<IBuildsAndStartsWorkflow>().BuildAndStartWorkflowAsync<Loop>();
await host.WaitForShutdownAsync();

public class Loop : IWorkflow
{
    public void Build(IWorkflowBuilder builder) =>
        builder
        .ForEach(x => new int[] { 10, 20, 30, 40 }, x =>
        {
            x.Then<Print>();
        });
}

public class Print : Activity
{
    protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
    {
        try
        {
            var index = context.GetVariable("CurrentIndex");
            var value = context.GetVariable("CurrentValue");
            System.Console.WriteLine($"Index {index} values {value} type {value.GetType()}");
            return Done("No");
        }
        catch(Exception ex)
        {
            return Fault(ex.Message);
        }
    }
}
@sfmskywalker sfmskywalker added bug Something isn't working prio high Is on the roadmap for the near-future labels Jul 3, 2021
@sfmskywalker sfmskywalker added this to the Elsa 2.1 milestone Jul 3, 2021
@sfmskywalker sfmskywalker self-assigned this Jul 3, 2021
@sfmskywalker sfmskywalker added this to Needs triage in Triage via automation Jul 3, 2021
@sfmskywalker sfmskywalker moved this from Needs triage to Triage complete in Triage Jul 3, 2021
Triage automation moved this from Triage complete to Closed Jul 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working prio high Is on the roadmap for the near-future
Projects
No open projects
Triage
Closed
Development

No branches or pull requests

1 participant