Add sample with custom activity and outcomes

This commit is contained in:
Sipke Schoorstra 2021-05-07 10:33:46 +02:00
parent 4848ebf41f
commit 0e9f139505
5 changed files with 108 additions and 0 deletions

View file

@ -273,6 +273,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Persistence.EntityFram
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.SendHttp", "src\samples\aspnet\Elsa.Samples.SendHttp\Elsa.Samples.SendHttp.csproj", "{775EFD59-F99C-4D38-B54E-9734C9D9649A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.CustomActivityOutcomes", "src\samples\console\Elsa.Samples.CustomActivityOutcomes\Elsa.Samples.CustomActivityOutcomes.csproj", "{30A9B140-14F5-4D00-84ED-1447F96D13C8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -651,6 +653,10 @@ Global
{775EFD59-F99C-4D38-B54E-9734C9D9649A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{775EFD59-F99C-4D38-B54E-9734C9D9649A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{775EFD59-F99C-4D38-B54E-9734C9D9649A}.Release|Any CPU.Build.0 = Release|Any CPU
{30A9B140-14F5-4D00-84ED-1447F96D13C8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{30A9B140-14F5-4D00-84ED-1447F96D13C8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{30A9B140-14F5-4D00-84ED-1447F96D13C8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{30A9B140-14F5-4D00-84ED-1447F96D13C8}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -778,6 +784,7 @@ Global
{3DF05CFC-AC77-489F-9A42-C4FDB599AA69} = {8B6B40A8-DF21-4CD2-BABD-474B79D0C3AF}
{D55BC08B-CA82-478A-99F7-EDE90AC33160} = {C865B0FD-E505-48F0-BFAF-0D4D7C1B5CA1}
{775EFD59-F99C-4D38-B54E-9734C9D9649A} = {22E75696-6FE9-436A-9097-EE21C603F818}
{30A9B140-14F5-4D00-84ED-1447F96D13C8} = {FC9F520F-BA51-4AD2-BFEE-EF787798E734}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8B0975FD-7050-48B0-88C5-48C33378E158}

View file

@ -0,0 +1,23 @@
using System;
using Elsa.ActivityResults;
using Elsa.Attributes;
using Elsa.Services;
namespace Elsa.Samples.CustomActivityOutcomes.Activities
{
[Action(Description = "Tries to perform some work. Returns Done when successful, Failed otherwise.", Outcomes = new[] { OutcomeNames.Done, "Failed" })]
public class TrySomething : Activity
{
private readonly Random _random;
public TrySomething() => _random = new Random();
protected override IActivityExecutionResult OnExecute()
{
// 50% chance of either returning Done or Failed.
var value = _random.Next(0, 2);
var outcome = value == 1 ? OutcomeNames.Done : "Failed";
return Outcome(outcome);
}
}
}

View file

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,27 @@
using System.Threading.Tasks;
using Elsa.Samples.CustomActivityOutcomes.Workflows;
using Elsa.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Samples.CustomActivityOutcomes
{
internal class Program
{
private static async Task Main()
{
// Create a service container with Elsa services.
var services = new ServiceCollection()
.AddElsa(options => options
.AddConsoleActivities()
.AddActivitiesFrom<Program>()
.AddWorkflowsFrom<Program>())
.BuildServiceProvider();
// Get a workflow runner.
var workflowRunner = services.GetRequiredService<IBuildsAndStartsWorkflow>();
// Run the workflow.
await workflowRunner.BuildAndStartWorkflowAsync<SampleWorkflow>();
}
}
}

View file

@ -0,0 +1,39 @@
using System;
using Elsa.Activities.Console;
using Elsa.Activities.ControlFlow;
using Elsa.Builders;
using Elsa.Samples.CustomActivityOutcomes.Activities;
namespace Elsa.Samples.CustomActivityOutcomes.Workflows
{
/// <summary>
/// Demonstrates connecting outcomes of activities to other activities.
/// </summary>
public class SampleWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder builder)
{
builder
.WriteLine("Trying something.").WithName("Start")
.Then<TrySomething>(trySomething =>
{
trySomething
.When(OutcomeNames.Done)
.WriteLine("Success!")
.ThenNamed("PromptTryAgain");
trySomething
.When("Failed")
.WriteLine("That didn't work. Better luck next time.")
.ThenNamed("PromptTryAgain");
})
.Add<WriteLine>(x => x.WithText("Try again? (y/n)")).WithName("PromptTryAgain")
.ReadLine()
.If(context => string.Equals(context.GetInput<string>(), "y", StringComparison.OrdinalIgnoreCase), @if =>
{
@if.When(OutcomeNames.True).ThenNamed("Start");
@if.When(OutcomeNames.False).WriteLine("See you around!");
});
}
}
}