diff --git a/src/modules/Elsa.MassTransit/Consumers/DispatchCancelWorkflowsRequestConsumer.cs b/src/modules/Elsa.MassTransit/Consumers/DispatchCancelWorkflowsRequestConsumer.cs
index f2e568d86..df7877eee 100644
--- a/src/modules/Elsa.MassTransit/Consumers/DispatchCancelWorkflowsRequestConsumer.cs
+++ b/src/modules/Elsa.MassTransit/Consumers/DispatchCancelWorkflowsRequestConsumer.cs
@@ -16,7 +16,6 @@ public class DispatchCancelWorkflowsRequestConsumer(IWorkflowRuntime workflowRun
{
var cancellationToken = context.CancellationToken;
var request = context.Message;
-
var client = await workflowRuntime.CreateClientAsync(request.WorkflowInstanceId, cancellationToken);
await client.CancelAsync(cancellationToken);
}
diff --git a/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowCancellationService.cs b/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowCancellationService.cs
index 73bfd514a..b7e32096d 100644
--- a/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowCancellationService.cs
+++ b/src/modules/Elsa.Workflows.Runtime/Contracts/IWorkflowCancellationService.cs
@@ -11,7 +11,7 @@ public interface IWorkflowCancellationService
/// Cancels a workflow instance.
///
/// Also cancels all children
- Task CancelWorkflowAsync(string workflowInstanceId, CancellationToken cancellationToken = default);
+ Task CancelWorkflowAsync(string workflowInstanceId, CancellationToken cancellationToken = default);
///
/// Cancels workflow executions with the specified workflow instance ID.
diff --git a/src/modules/Elsa.Workflows.Runtime/Notifications/WorkflowCancelled.cs b/src/modules/Elsa.Workflows.Runtime/Notifications/WorkflowCancelled.cs
new file mode 100644
index 000000000..266922377
--- /dev/null
+++ b/src/modules/Elsa.Workflows.Runtime/Notifications/WorkflowCancelled.cs
@@ -0,0 +1,5 @@
+using Elsa.Mediator.Contracts;
+
+namespace Elsa.Workflows.Runtime.Notifications;
+
+public record WorkflowCancelled(string WorkflowInstanceId) : INotification;
\ No newline at end of file
diff --git a/src/modules/Elsa.Workflows.Runtime/Notifications/WorkflowCancelling.cs b/src/modules/Elsa.Workflows.Runtime/Notifications/WorkflowCancelling.cs
new file mode 100644
index 000000000..ba3d447f1
--- /dev/null
+++ b/src/modules/Elsa.Workflows.Runtime/Notifications/WorkflowCancelling.cs
@@ -0,0 +1,5 @@
+using Elsa.Mediator.Contracts;
+
+namespace Elsa.Workflows.Runtime.Notifications;
+
+public record WorkflowCancelling(string WorkflowInstanceId) : INotification;
\ No newline at end of file
diff --git a/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCanceler.cs b/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCanceler.cs
index 33adf4000..8c612e6fb 100644
--- a/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCanceler.cs
+++ b/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCanceler.cs
@@ -1,39 +1,36 @@
+using Elsa.Mediator.Contracts;
using Elsa.Workflows.Models;
using Elsa.Workflows.Pipelines.WorkflowExecution;
using Elsa.Workflows.Runtime.Middleware.Workflows;
+using Elsa.Workflows.Runtime.Notifications;
using Elsa.Workflows.State;
namespace Elsa.Workflows.Runtime;
///
-public class WorkflowCanceler(IWorkflowExecutionPipeline workflowExecutionPipeline, IWorkflowStateExtractor workflowStateExtractor, IServiceProvider serviceProvider) : IWorkflowCanceler
+public class WorkflowCanceler(
+ IWorkflowExecutionPipeline workflowExecutionPipeline,
+ IWorkflowStateExtractor workflowStateExtractor,
+ IMediator mediator,
+ IServiceProvider serviceProvider) : IWorkflowCanceler
{
///
public async Task CancelWorkflowAsync(WorkflowGraph workflowGraph, WorkflowState workflowState, CancellationToken cancellationToken = default)
{
var workflowExecutionContext = await WorkflowExecutionContext.CreateAsync(serviceProvider, workflowGraph, workflowState, cancellationToken: cancellationToken);
-
- // Alter the workflow execution context to cancel the workflow.
await CancelWorkflowAsync(workflowExecutionContext, cancellationToken);
-
- // Map the workflow execution context back to a workflow state.
return workflowStateExtractor.Extract(workflowExecutionContext);
}
///
public async Task CancelWorkflowAsync(WorkflowExecutionContext workflowExecutionContext, CancellationToken cancellationToken = default)
{
- // Build a new workflow execution pipeline.
+ await mediator.SendAsync(new WorkflowCancelling(workflowExecutionContext.Id), cancellationToken);
var pipelineBuilder = new WorkflowExecutionPipelineBuilder(serviceProvider);
workflowExecutionPipeline.ConfigurePipelineBuilder(pipelineBuilder);
-
- // Replace the terminal DefaultActivitySchedulerMiddleware with the CancelWorkflowMiddleware terminal.
pipelineBuilder.ReplaceTerminal();
-
- // Build modified pipeline.
var pipeline = pipelineBuilder.Build();
-
- // Execute the pipeline.
await pipeline(workflowExecutionContext);
+ await mediator.SendAsync(new WorkflowCancelled(workflowExecutionContext.Id), cancellationToken);
}
}
\ No newline at end of file
diff --git a/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCancellationService.cs b/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCancellationService.cs
index 7e02f7bcd..77aaa3c83 100644
--- a/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCancellationService.cs
+++ b/src/modules/Elsa.Workflows.Runtime/Services/WorkflowCancellationService.cs
@@ -15,7 +15,7 @@ public class WorkflowCancellationService(
: IWorkflowCancellationService
{
///
- public async Task CancelWorkflowAsync(string workflowInstanceId, CancellationToken cancellationToken = default)
+ public async Task CancelWorkflowAsync(string workflowInstanceId, CancellationToken cancellationToken = default)
{
var filter = new WorkflowInstanceFilter
{
@@ -23,12 +23,11 @@ public class WorkflowCancellationService(
};
var instance = await workflowInstanceStore.FindAsync(filter, cancellationToken);
- return instance == null
- ? 0
- : await CancelWorkflows(new List
- {
- instance
- }, cancellationToken);
+ if(instance == null)
+ return false;
+
+ await CancelWorkflows([instance], cancellationToken);
+ return true;
}
///