Add composite activity output sample
This commit is contained in:
parent
6e014158c1
commit
dbeda1961f
|
|
@ -0,0 +1,42 @@
|
|||
using System;
|
||||
using Elsa.Activities.ControlFlow;
|
||||
using Elsa.Attributes;
|
||||
using Elsa.Builders;
|
||||
using Elsa.Services;
|
||||
|
||||
namespace Elsa.Samples.ProgrammaticCompositeActivitiesConsole.Activities
|
||||
{
|
||||
/// <summary>
|
||||
/// Demonstrates accepting input properties and returning an output value from a composite activity.
|
||||
/// </summary>
|
||||
[Action(Outcomes = new[] { "Left", "Right" })]
|
||||
public class Sum : CompositeActivity
|
||||
{
|
||||
[ActivityInput]
|
||||
public int A
|
||||
{
|
||||
get => GetState<int>();
|
||||
set => SetState(value);
|
||||
}
|
||||
|
||||
[ActivityInput]
|
||||
public int B
|
||||
{
|
||||
get => GetState<int>();
|
||||
set => SetState(value);
|
||||
}
|
||||
|
||||
[ActivityOutput]
|
||||
public int Result
|
||||
{
|
||||
get => GetState<int>();
|
||||
set => SetState(value);
|
||||
}
|
||||
|
||||
public override void Build(ICompositeActivityBuilder builder)
|
||||
{
|
||||
builder
|
||||
.StartWith(c => Result = A + B);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -7,13 +7,13 @@
|
|||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="System.Text.Encodings.Web" Version="5.0.1"/>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0"/>
|
||||
<PackageReference Include="System.Text.Encodings.Web" Version="5.0.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" />
|
||||
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="5.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj"/>
|
||||
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
|
|
|||
|
|
@ -16,9 +16,8 @@ namespace Elsa.Samples.ProgrammaticCompositeActivitiesConsole
|
|||
.AddLogging(logging => logging.AddConsole().SetMinimumLevel(LogLevel.Warning))
|
||||
.AddElsa(options => options
|
||||
.AddConsoleActivities()
|
||||
.AddActivity<NavigateActivity>()
|
||||
.AddActivity<CountdownActivity>()
|
||||
.AddWorkflow<CompositionWorkflow>())
|
||||
.AddActivitiesFrom<Program>()
|
||||
.AddWorkflowsFrom<Program>())
|
||||
.BuildServiceProvider();
|
||||
|
||||
// Run startup actions (not needed when registering Elsa with a Host).
|
||||
|
|
|
|||
|
|
@ -20,6 +20,24 @@ namespace Elsa.Samples.ProgrammaticCompositeActivitiesConsole.Workflows
|
|||
countDown.When("Right").WriteLine("Where going right!").ThenNamed("CountdownDemo");
|
||||
})
|
||||
.WriteLine("=Countdown demo=").WithName("CountdownDemo")
|
||||
.Then<CountdownActivity>(activity => activity.Set(x => x.Start, 10));
|
||||
.Then<CountdownActivity>(activity => activity.Set(x => x.Start, 10))
|
||||
.WriteLine("=Sum demo=").WithName("SumDemo")
|
||||
.WriteLine("Enter first value:")
|
||||
.ReadLine().WithName("ValueA")
|
||||
.WriteLine("Enter second value:")
|
||||
.ReadLine().WithName("ValueB")
|
||||
.Then<Sum>(activity => activity
|
||||
.Set(x => x.A, async context =>
|
||||
{
|
||||
var a = (await context.WorkflowExecutionContext.GetNamedActivityPropertyAsync<ReadLine, string>("ValueA", r => r.Output))!;
|
||||
return int.Parse(a);
|
||||
})
|
||||
.Set(x => x.B, async context =>
|
||||
{
|
||||
var b = (await context.WorkflowExecutionContext.GetNamedActivityPropertyAsync<ReadLine, string>("ValueB", r => r.Output))!;
|
||||
return int.Parse(b);
|
||||
})
|
||||
).WithName("Sum1")
|
||||
.WriteLine(async context => $"Sum: {await context.GetNamedActivityPropertyAsync<Sum, int>("Sum1", x => x.Result)}");
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue