Skip to content

Commit a4f6f92

Browse files
authored
Merge pull request #710 from UltimateSoftware/feature/ScopedMiddleware
Feature - Scoped Middleware
2 parents fcb1434 + 910974b commit a4f6f92

File tree

5 files changed

+30
-19
lines changed

5 files changed

+30
-19
lines changed

src/WorkflowCore/ServiceCollectionExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ public static IServiceCollection AddWorkflow(this IServiceCollection services, A
5151
services.AddSingleton<IGreyList, GreyList>();
5252
services.AddSingleton<IWorkflowController, WorkflowController>();
5353
services.AddSingleton<IActivityController, ActivityController>();
54-
services.AddSingleton<IStepExecutor, StepExecutor>();
55-
services.AddSingleton<IWorkflowMiddlewareRunner, WorkflowMiddlewareRunner>();
56-
services.AddSingleton<IWorkflowMiddlewareErrorHandler, DefaultWorkflowMiddlewareErrorHandler>();
5754
services.AddSingleton<IWorkflowHost, WorkflowHost>();
55+
services.AddTransient<IStepExecutor, StepExecutor>();
56+
services.AddTransient<IWorkflowMiddlewareErrorHandler, DefaultWorkflowMiddlewareErrorHandler>();
57+
services.AddTransient<IWorkflowMiddlewareRunner, WorkflowMiddlewareRunner>();
5858
services.AddTransient<IScopeProvider, ScopeProvider>();
5959
services.AddTransient<IWorkflowExecutor, WorkflowExecutor>();
6060
services.AddTransient<ICancellationProcessor, CancellationProcessor>();

src/WorkflowCore/Services/WorkflowController.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@ public class WorkflowController : IWorkflowController
2121
private readonly IExecutionPointerFactory _pointerFactory;
2222
private readonly ILifeCycleEventHub _eventHub;
2323
private readonly IServiceProvider _serviceProvider;
24-
private readonly IWorkflowMiddlewareRunner _middlewareRunner;
2524
private readonly ILogger _logger;
2625

27-
public WorkflowController(IPersistenceProvider persistenceStore, IDistributedLockProvider lockProvider, IWorkflowRegistry registry, IQueueProvider queueProvider, IExecutionPointerFactory pointerFactory, ILifeCycleEventHub eventHub, ILoggerFactory loggerFactory, IServiceProvider serviceProvider, IWorkflowMiddlewareRunner middlewareRunner)
26+
public WorkflowController(IPersistenceProvider persistenceStore, IDistributedLockProvider lockProvider, IWorkflowRegistry registry, IQueueProvider queueProvider, IExecutionPointerFactory pointerFactory, ILifeCycleEventHub eventHub, ILoggerFactory loggerFactory, IServiceProvider serviceProvider)
2827
{
2928
_persistenceStore = persistenceStore;
3029
_lockProvider = lockProvider;
@@ -33,7 +32,6 @@ public WorkflowController(IPersistenceProvider persistenceStore, IDistributedLoc
3332
_pointerFactory = pointerFactory;
3433
_eventHub = eventHub;
3534
_serviceProvider = serviceProvider;
36-
_middlewareRunner = middlewareRunner;
3735
_logger = loggerFactory.CreateLogger<WorkflowController>();
3836
}
3937

@@ -85,7 +83,11 @@ public async Task<string> StartWorkflow<TData>(string workflowId, int? version,
8583

8684
wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def));
8785

88-
await _middlewareRunner.RunPreMiddleware(wf, def);
86+
using (var scope = _serviceProvider.CreateScope())
87+
{
88+
var middlewareRunner = scope.ServiceProvider.GetRequiredService<IWorkflowMiddlewareRunner>();
89+
await middlewareRunner.RunPreMiddleware(wf, def);
90+
}
8991

9092
string id = await _persistenceStore.CreateNewWorkflow(wf);
9193
await _queueProvider.QueueWork(id, QueueType.Workflow);

src/WorkflowCore/Services/WorkflowExecutor.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@ public class WorkflowExecutor : IWorkflowExecutor
2222
private readonly ICancellationProcessor _cancellationProcessor;
2323
private readonly ILifeCycleEventPublisher _publisher;
2424
private readonly WorkflowOptions _options;
25-
private readonly IStepExecutor _stepExecutor;
26-
private readonly IWorkflowMiddlewareRunner _middlewareRunner;
2725

2826
private IWorkflowHost Host => _serviceProvider.GetService<IWorkflowHost>();
2927

30-
public WorkflowExecutor(IWorkflowRegistry registry, IServiceProvider serviceProvider, IScopeProvider scopeProvider, IDateTimeProvider datetimeProvider, IExecutionResultProcessor executionResultProcessor, ILifeCycleEventPublisher publisher, ICancellationProcessor cancellationProcessor, WorkflowOptions options, IWorkflowMiddlewareRunner middlewareRunner, IStepExecutor stepExecutor, ILoggerFactory loggerFactory)
28+
public WorkflowExecutor(IWorkflowRegistry registry, IServiceProvider serviceProvider, IScopeProvider scopeProvider, IDateTimeProvider datetimeProvider, IExecutionResultProcessor executionResultProcessor, ILifeCycleEventPublisher publisher, ICancellationProcessor cancellationProcessor, WorkflowOptions options, ILoggerFactory loggerFactory)
3129
{
3230
_serviceProvider = serviceProvider;
3331
_scopeProvider = scopeProvider;
@@ -38,8 +36,6 @@ public WorkflowExecutor(IWorkflowRegistry registry, IServiceProvider serviceProv
3836
_options = options;
3937
_logger = loggerFactory.CreateLogger<WorkflowExecutor>();
4038
_executionResultProcessor = executionResultProcessor;
41-
_middlewareRunner = middlewareRunner;
42-
_stepExecutor = stepExecutor;
4339
}
4440

4541
public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow, CancellationToken cancellationToken = default)
@@ -157,6 +153,7 @@ private async Task ExecuteStep(WorkflowInstance workflow, WorkflowStep step, Exe
157153
_logger.LogDebug("Starting step {0} on workflow {1}", step.Name, workflow.Id);
158154

159155
IStepBody body = step.ConstructBody(scope.ServiceProvider);
156+
var stepExecutor = scope.ServiceProvider.GetRequiredService<IStepExecutor>();
160157

161158
if (body == null)
162159
{
@@ -185,7 +182,7 @@ private async Task ExecuteStep(WorkflowInstance workflow, WorkflowStep step, Exe
185182
return;
186183
}
187184

188-
var result = await _stepExecutor.ExecuteStep(context, body);
185+
var result = await stepExecutor.ExecuteStep(context, body);
189186

190187
if (result.Proceed)
191188
{
@@ -250,7 +247,11 @@ private async Task DetermineNextExecutionTime(WorkflowInstance workflow, Workflo
250247
workflow.Status = WorkflowStatus.Complete;
251248
workflow.CompleteTime = _datetimeProvider.UtcNow;
252249

253-
await _middlewareRunner.RunPostMiddleware(workflow, def);
250+
using (var scope = _serviceProvider.CreateScope())
251+
{
252+
var middlewareRunner = scope.ServiceProvider.GetRequiredService<IWorkflowMiddlewareRunner>();
253+
await middlewareRunner.RunPostMiddleware(workflow, def);
254+
}
254255

255256
_publisher.PublishNotification(new WorkflowCompleted()
256257
{

src/WorkflowCore/WorkflowCore.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute>
1616
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
1717
<Description>Workflow Core is a light weight workflow engine targeting .NET Standard.</Description>
18-
<Version>3.3.2</Version>
19-
<AssemblyVersion>3.3.2.0</AssemblyVersion>
20-
<FileVersion>3.3.2.0</FileVersion>
18+
<Version>3.3.3</Version>
19+
<AssemblyVersion>3.3.3.0</AssemblyVersion>
20+
<FileVersion>3.3.3.0</FileVersion>
2121
<PackageReleaseNotes></PackageReleaseNotes>
2222
<PackageIconUrl>https://github.com/danielgerlag/workflow-core/raw/master/src/logo.png</PackageIconUrl>
23-
<PackageVersion>3.3.2</PackageVersion>
23+
<PackageVersion>3.3.3</PackageVersion>
2424
</PropertyGroup>
2525

2626
<ItemGroup>

test/WorkflowCore.UnitTests/Services/WorkflowExecutorFixture.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public WorkflowExecutorFixture()
5151
A.CallTo(() => DateTimeProvider.Now).Returns(DateTime.Now);
5252
A.CallTo(() => DateTimeProvider.UtcNow).Returns(DateTime.UtcNow);
5353

54+
A
55+
.CallTo(() => ServiceProvider.GetService(typeof(IWorkflowMiddlewareRunner)))
56+
.Returns(MiddlewareRunner);
57+
58+
A
59+
.CallTo(() => ServiceProvider.GetService(typeof(IStepExecutor)))
60+
.Returns(StepExecutor);
61+
5462
A.CallTo(() => MiddlewareRunner
5563
.RunPostMiddleware(A<WorkflowInstance>._, A<WorkflowDefinition>._))
5664
.Returns(Task.CompletedTask);
@@ -64,7 +72,7 @@ public WorkflowExecutorFixture()
6472
var loggerFactory = new LoggerFactory();
6573
//loggerFactory.AddConsole(LogLevel.Debug);
6674

67-
Subject = new WorkflowExecutor(Registry, ServiceProvider, ScopeProvider, DateTimeProvider, ResultProcesser, EventHub, CancellationProcessor, Options, MiddlewareRunner, StepExecutor, loggerFactory);
75+
Subject = new WorkflowExecutor(Registry, ServiceProvider, ScopeProvider, DateTimeProvider, ResultProcesser, EventHub, CancellationProcessor, Options, loggerFactory);
6876
}
6977

7078
[Fact(DisplayName = "Should execute active step")]

0 commit comments

Comments
 (0)