Created
July 12, 2021 13:07
-
-
Save jdevillard/6eb970d25dc0a5f993a072196bbfdff0 to your computer and use it in GitHub Desktop.
Signaling Workflow using CorrelationId or WorkflowId
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Elsa.Activities.Signaling.Services; | |
using Elsa.ActivityResults; | |
using Elsa.Builders; | |
using Elsa.Services; | |
using Elsa.Services.Models; | |
using Microsoft.Extensions.DependencyInjection; | |
using Elsa.Activities.Console; | |
using Elsa.Activities.ControlFlow; | |
using MediatR; | |
using Elsa.Events; | |
using System.Threading; | |
using System.Linq; | |
namespace Elsa.Samples.SignalingConsole | |
{ | |
/// <summary> | |
/// Demonstrates a workflow with a While looping construct. | |
/// </summary> | |
internal static class Program | |
{ | |
private static async Task Main() | |
{ | |
// Create a service container with Elsa services. | |
var services = new ServiceCollection() | |
.AddElsa(options => options | |
.AddConsoleActivities() | |
.AddWorkflowsFrom<SignalReceiverWorkflow>() | |
.AddActivitiesFrom<SignalReceiverWorkflow>() | |
) | |
.BuildServiceProvider(); | |
// Run startup actions (will index triggers such as SignalReceived from workflows). | |
var startupRunner = services.GetRequiredService<IStartupRunner>(); | |
await startupRunner.StartupAsync(); | |
Console.WriteLine("Send UserWorkflow Signal"); | |
var signaler = services.GetRequiredService<ISignaler>(); | |
await signaler.TriggerSignalAsync("UserWorkflow"); | |
// Keep the application alive for the workflow scheduler to have enough time to resume the workflow. | |
Console.WriteLine("paste cor:{correlationid} or {instanceId} to continue"); | |
var instanceIdorCorrelationId = Console.ReadLine(); | |
if (instanceIdorCorrelationId.StartsWith("cor:")) | |
{ | |
var correlationId = instanceIdorCorrelationId.Substring(4); | |
var result = await signaler.TriggerSignalAsync("AdminSignal", correlationId: correlationId); | |
Console.WriteLine($" found {result.ToArray().Length} using correlationId"); | |
} | |
else | |
{ | |
var result = await signaler.TriggerSignalAsync("AdminSignal", workflowInstanceId: instanceIdorCorrelationId); | |
Console.WriteLine($" found {result.ToArray().Length} using instanceId"); | |
} | |
Console.ReadLine(); | |
} | |
} | |
public class RegisterWorkflow : IWorkflow | |
{ | |
public void Build(IWorkflowBuilder builder) | |
{ | |
builder | |
.SignalReceived("UserWorkflow") | |
.Then<StartRegisterActivity>() | |
.WriteLine((ctx) => $"Signal received! -- Now Registering Activity and Waiting for admin signal with Instance Id {ctx.WorkflowInstance.Id}") | |
.WriteLine((ctx) => $"Signal received! -- Now Registering Activity and Waiting for admin signal with Correlation Id {ctx.CorrelationId}") | |
.SignalReceived("AdminSignal") | |
.WriteLine(() => $"Signal From Admin Received") | |
.Then<CompleteRegistrationAfterAdminActivity>() | |
.WriteLine(() => $"End of Worflow") | |
; | |
} | |
} | |
public class StartRegisterActivity : Elsa.Services.Activity | |
{ | |
protected override IActivityExecutionResult OnExecute() | |
{ | |
Console.WriteLine("doing something to start the registration process and wait for validation"); | |
return Done(); | |
} | |
} | |
public class CompleteRegistrationAfterAdminActivity : Elsa.Services.Activity | |
{ | |
protected override IActivityExecutionResult OnExecute() | |
{ | |
Console.WriteLine("doing something to validate the registration after Admin Signal"); | |
return Done(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment