Skip to content

Instantly share code, notes, and snippets.

@sfmskywalker
Last active August 3, 2021 08:43
Show Gist options
  • Select an option

  • Save sfmskywalker/ab8abe1021e6632b5bcff99116ad6e23 to your computer and use it in GitHub Desktop.

Select an option

Save sfmskywalker/ab8abe1021e6632b5bcff99116ad6e23 to your computer and use it in GitHub Desktop.
Building Workflow Driven .NET Applications With Elsa 2 - Part 5
using System.Threading;
using System.Threading.Tasks;
using DocumentManagement.Core.Events;
using Elsa.Models;
using Elsa.Services;
using MediatR;
namespace DocumentManagement.Workflows.Handlers
{
/// <summary>
/// Handles the <see cref="NewDocumentReceived"/> event by starting the HelloFile workflow.
/// </summary>
public class StartHelloFileWorkflow : INotificationHandler<NewDocumentReceived>
{
private readonly IWorkflowRegistry _workflowRegistry;
private readonly IWorkflowDefinitionDispatcher _workflowDispatcher;
public StartHelloFileWorkflow(IWorkflowRegistry workflowRegistry, IWorkflowDefinitionDispatcher workflowDispatcher)
{
_workflowRegistry = workflowRegistry;
_workflowDispatcher = workflowDispatcher;
}
public async Task Handle(NewDocumentReceived notification, CancellationToken cancellationToken)
{
var document = notification.Document;
// Get our HelloFile workflow.
var workflow =
await _workflowRegistry.GetAsync("HelloFile", default, VersionOptions.Published, cancellationToken);
if (workflow == null)
return; // Do nothing.
// Dispatch the workflow.
await _workflowDispatcher.DispatchAsync(new ExecuteWorkflowDefinitionRequest(workflow!.Id, Input: new WorkflowInput(document.Id)), cancellationToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment