2022-11-10 13:40:37 +00:00
|
|
|
|
using Elsa.Extensions;
|
|
|
|
|
|
using Elsa.Workflows.Core.Activities;
|
2023-03-03 21:47:23 +00:00
|
|
|
|
using Elsa.Workflows.Core.Contracts;
|
2022-11-10 13:40:37 +00:00
|
|
|
|
using Elsa.Workflows.Core.Models;
|
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
|
|
|
|
|
|
|
// Setup service container.
|
|
|
|
|
|
var services = new ServiceCollection();
|
|
|
|
|
|
|
|
|
|
|
|
// Add Elsa services.
|
|
|
|
|
|
services.AddElsa();
|
|
|
|
|
|
|
|
|
|
|
|
// Build service container.
|
|
|
|
|
|
var serviceProvider = services.BuildServiceProvider();
|
|
|
|
|
|
|
|
|
|
|
|
// Declare a workflow variable for use in the workflow.
|
|
|
|
|
|
var ageVariable = new Variable<string>();
|
|
|
|
|
|
|
|
|
|
|
|
// Declare a workflow.
|
|
|
|
|
|
var workflow = new Sequence
|
|
|
|
|
|
{
|
|
|
|
|
|
// Register the variable.
|
|
|
|
|
|
Variables = { ageVariable },
|
|
|
|
|
|
|
|
|
|
|
|
// Setup the sequence of activities to run.
|
|
|
|
|
|
Activities =
|
|
|
|
|
|
{
|
|
|
|
|
|
new WriteLine("Please tell me your age:"),
|
|
|
|
|
|
new ReadLine(ageVariable), // Stores user input into the provided variable.,
|
|
|
|
|
|
new If
|
|
|
|
|
|
{
|
|
|
|
|
|
// If aged 18 or up, beer is provided, soda otherwise.
|
2023-04-21 22:02:02 +00:00
|
|
|
|
Condition = new (context => ageVariable.Get<int>(context) < 18),
|
2022-11-10 13:40:37 +00:00
|
|
|
|
Then = new WriteLine("Enjoy your soda!"),
|
|
|
|
|
|
Else = new WriteLine("Enjoy your beer!")
|
|
|
|
|
|
},
|
|
|
|
|
|
new WriteLine("Come again!")
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// Resolve a workflow runner to run the workflow.
|
|
|
|
|
|
var workflowRunner = serviceProvider.GetRequiredService<IWorkflowRunner>();
|
|
|
|
|
|
|
|
|
|
|
|
// Run the workflow.
|
|
|
|
|
|
await workflowRunner.RunAsync(workflow);
|