Skip to content

Instantly share code, notes, and snippets.

@sfmskywalker
Last active August 3, 2021 08:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfmskywalker/ab8abe1021e6632b5bcff99116ad6e23 to your computer and use it in GitHub Desktop.
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;
using Open.Linq.AsyncExtensions;
namespace DocumentManagement.Workflows.Handlers
{
/// <summary>
/// Handles the <see cref="NewDocumentReceived"/> event by starting new workflows associated with the document type.
/// </summary>
public class StartDocumentWorkflows : INotificationHandler<NewDocumentReceived>
{
private readonly IWorkflowRegistry _workflowRegistry;
private readonly IWorkflowDefinitionDispatcher _workflowDispatcher;
public StartDocumentWorkflows(IWorkflowRegistry workflowRegistry, IWorkflowDefinitionDispatcher workflowDispatcher)
{
_workflowRegistry = workflowRegistry;
_workflowDispatcher = workflowDispatcher;
}
public async Task Handle(NewDocumentReceived notification, CancellationToken cancellationToken)
{
var document = notification.Document;
var documentTypeId = document.DocumentTypeId;
// Get all workflow blueprints tagged with the received document type ID.
var workflowBlueprints = await _workflowRegistry.FindManyAsync(x => x.IsPublished && x.Tag == documentTypeId, cancellationToken).ToList();
// Dispatch each workflow. Each workflow will be correlated by Document ID.
foreach (var workflowBlueprint in workflowBlueprints)
await _workflowDispatcher.DispatchAsync(new ExecuteWorkflowDefinitionRequest(workflowBlueprint.Id, CorrelationId: document.Id, Input: new WorkflowInput(document.Id)), cancellationToken);
}
}
}
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.FindAsync(x => x.Name == "HelloFile" && x.IsPublished, 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