Implementation notes using Esla 2.0 #760
Replies: 10 comments 22 replies
-
OK so it seems to me that the most prudent way is to use the latest build of Esla 2.0 #347 (reply in thread) |
Beta Was this translation helpful? Give feedback.
-
Hello world with Esla 2 using System;
using Elsa.Activities.Console;
using Elsa.Builders;
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
var services = new ServiceCollection()
.AddElsaCore(options => options.
AddConsoleActivities()
.AddWorkflow<HelloWorld>())
.BuildServiceProvider();
var workflowRunner = services.GetRequiredService<IBuildsAndStartsWorkflow>();
await workflowRunner.BuildAndStartWorkflowAsync<HelloWorld>();
public class HelloWorld : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder.WriteLine("Hello world 2");
}
} |
Beta Was this translation helpful? Give feedback.
-
This doesn't work - it just prints using System;
using Elsa.Activities.Console;
using Elsa.Builders;
using Elsa.Services;
using Elsa.Activities.Temporal;
using Microsoft.Extensions.DependencyInjection;
using NodaTime;
using Elsa;
var services = new ServiceCollection()
.AddElsaCore(options => options.
AddConsoleActivities()
.AddWorkflow<HelloWorld>()
.AddQuartzTemporalActivities()
)
.BuildServiceProvider();
var workflowRunner = services.GetRequiredService<IBuildsAndStartsWorkflow>();
await workflowRunner.BuildAndStartWorkflowAsync<HelloWorld>();
Console.ReadLine();
public class HelloWorld : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
int count = 0;
builder.Timer(Duration.FromSeconds(5))
.WriteLine($"Hello world {count++}");
}
} |
Beta Was this translation helpful? Give feedback.
-
We can set a variable from the workflow and use it within activity. using Elsa;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Elsa.Builders;
using Elsa.Services;
using Elsa.ActivityResults;
using Elsa.Services.Models;
using var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddElsa(elsa =>
{
elsa
.AddConsoleActivities()
.AddQuartzTemporalActivities()
.AddWorkflow<HelloWorld>()
.AddActivity<SayHello>();
});
})
.UseConsoleLifetime()
.Build();
await host.Services.GetService<IBuildsAndStartsWorkflow>().BuildAndStartWorkflowAsync<HelloWorld>();
await host.WaitForShutdownAsync();
public class HelloWorld : IWorkflow
{
public void Build(IWorkflowBuilder builder) =>
builder
.SetVariable("message", "hello")
.Then<SayHello>();
}
public class SayHello : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
var message = context.GetVariable<string>("message");
System.Console.WriteLine(message);
return Done();
}
} |
Beta Was this translation helpful? Give feedback.
-
Call one activity then another 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 var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddElsa(elsa =>
{
elsa
.AddConsoleActivities()
.AddQuartzTemporalActivities()
.AddWorkflow<HelloWorld>()
.AddActivity<SayHello>()
.AddActivity<SayGoodbye>();
});
})
.UseConsoleLifetime()
.Build();
await host.Services.GetService<IBuildsAndStartsWorkflow>().BuildAndStartWorkflowAsync<HelloWorld>();
await host.WaitForShutdownAsync();
public class HelloWorld : IWorkflow
{
public void Build(IWorkflowBuilder builder) =>
builder
.SetVariable("message", "hello")
.Then<SayHello>()
.Then<SayGoodbye>();
}
public class SayHello : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
try
{
var message = context.GetVariable<string>("message");
System.Console.WriteLine(message);
context.SetVariable("message", "goodbye");
return Outcome(OutcomeNames.Done);
}
catch(Exception ex)
{
return Fault(ex.Message);
}
}
}
public class SayGoodbye : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
try
{
var message = context.GetVariable<string>("message");
System.Console.WriteLine(message);
return Outcome(OutcomeNames.Done);
}
catch (Exception ex)
{
return Fault(ex.Message);
}
}
} Pay attention |
Beta Was this translation helpful? Give feedback.
-
You can return output object when you return 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 var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddElsa(elsa =>
{
elsa
.AddConsoleActivities()
.AddQuartzTemporalActivities()
.AddWorkflow<HelloWorld>()
.AddActivity<SayHello>()
.AddActivity<SayGoodbye>();
});
})
.UseConsoleLifetime()
.Build();
await host.Services.GetService<IBuildsAndStartsWorkflow>().BuildAndStartWorkflowAsync<HelloWorld>();
await host.WaitForShutdownAsync();
public class HelloWorld : IWorkflow
{
public void Build(IWorkflowBuilder builder) =>
builder
.SetVariable("message", "hello")
.Then<SayHello>()
.Then<SayGoodbye>();
}
public class SayHello : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
try
{
var message = context.GetVariable<string>("message");
System.Console.WriteLine(message);
return Done("goodbye");
}
catch(Exception ex)
{
return Fault(ex.Message);
}
}
}
public class SayGoodbye : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
try
{
var message = context.GetInput<string>();
System.Console.WriteLine("What is the message? " + message);
return Outcome(OutcomeNames.Done);
}
catch (Exception ex)
{
return Fault(ex.Message);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Esla Default ActivitiesSetVariable
This activity assign variable with value in Then
Activtiy is only executed if the previous Outcome is |
Beta Was this translation helpful? Give feedback.
-
LoopThis executes 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 var host = new HostBuilder()
.ConfigureServices(services =>
{
services.AddElsa(elsa =>
{
elsa
.AddConsoleActivities()
.AddQuartzTemporalActivities()
.AddWorkflow<Loop>()
.AddActivity<SayHello>();
});
})
.UseConsoleLifetime()
.Build();
await host.Services.GetService<IBuildsAndStartsWorkflow>().BuildAndStartWorkflowAsync<Loop>();
await host.WaitForShutdownAsync();
public class Loop : IWorkflow
{
public void Build(IWorkflowBuilder builder) =>
builder
.For(start: 0, end: 4, x => x.Then<SayHello>());
}
public class SayHello : Activity
{
protected override IActivityExecutionResult OnExecute(ActivityExecutionContext context)
{
try
{
System.Console.WriteLine("Say Hello");
return Done();
}
catch(Exception ex)
{
return Fault(ex.Message);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
ForeachThis will print the index, the value and the type of the given collection. 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 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);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Why does in this workflow "CurrentValue" variable is missing from 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);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I need a simple workflow for our internal recruitment system to handle customizable email notifications when certain events happen (new submission, new file submission) and also to configure automatic tagging.
This thread will just contain things that I found while learning and integrating Esla to this small application.
Beta Was this translation helpful? Give feedback.
All reactions