@@ -14,6 +14,7 @@ public class WorkflowExecutor : IWorkflowExecutor
14
14
{
15
15
protected readonly IWorkflowRegistry _registry ;
16
16
protected readonly IServiceProvider _serviceProvider ;
17
+ protected readonly IScopeProvider _scopeProvider ;
17
18
protected readonly IDateTimeProvider _datetimeProvider ;
18
19
protected readonly ILogger _logger ;
19
20
private readonly IExecutionResultProcessor _executionResultProcessor ;
@@ -23,9 +24,10 @@ public class WorkflowExecutor : IWorkflowExecutor
23
24
24
25
private IWorkflowHost Host => _serviceProvider . GetService < IWorkflowHost > ( ) ;
25
26
26
- public WorkflowExecutor ( IWorkflowRegistry registry , IServiceProvider serviceProvider , IDateTimeProvider datetimeProvider , IExecutionResultProcessor executionResultProcessor , ILifeCycleEventPublisher publisher , ICancellationProcessor cancellationProcessor , WorkflowOptions options , ILoggerFactory loggerFactory )
27
+ public WorkflowExecutor ( IWorkflowRegistry registry , IServiceProvider serviceProvider , IScopeProvider scopeProvider , IDateTimeProvider datetimeProvider , IExecutionResultProcessor executionResultProcessor , ILifeCycleEventPublisher publisher , ICancellationProcessor cancellationProcessor , WorkflowOptions options , ILoggerFactory loggerFactory )
27
28
{
28
29
_serviceProvider = serviceProvider ;
30
+ _scopeProvider = scopeProvider ;
29
31
_registry = registry ;
30
32
_datetimeProvider = datetimeProvider ;
31
33
_publisher = publisher ;
@@ -87,57 +89,60 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
87
89
pointer . StartTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
88
90
}
89
91
90
- _logger . LogDebug ( "Starting step {0} on workflow {1}" , step . Name , workflow . Id ) ;
92
+ using ( var scope = _scopeProvider . CreateScope ( ) )
93
+ {
94
+ _logger . LogDebug ( "Starting step {0} on workflow {1}" , step . Name , workflow . Id ) ;
91
95
92
- IStepBody body = step . ConstructBody ( _serviceProvider ) ;
96
+ IStepBody body = step . ConstructBody ( scope . ServiceProvider ) ;
93
97
94
- if ( body == null )
95
- {
96
- _logger . LogError ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) ) ;
97
- pointer . SleepUntil = _datetimeProvider . Now . ToUniversalTime ( ) . Add ( _options . ErrorRetryInterval ) ;
98
- wfResult . Errors . Add ( new ExecutionError ( )
98
+ if ( body == null )
99
99
{
100
- WorkflowId = workflow . Id ,
101
- ExecutionPointerId = pointer . Id ,
102
- ErrorTime = _datetimeProvider . Now . ToUniversalTime ( ) ,
103
- Message = String . Format ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) )
104
- } ) ;
105
- continue ;
106
- }
100
+ _logger . LogError ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) ) ;
101
+ pointer . SleepUntil = _datetimeProvider . Now . ToUniversalTime ( ) . Add ( _options . ErrorRetryInterval ) ;
102
+ wfResult . Errors . Add ( new ExecutionError ( )
103
+ {
104
+ WorkflowId = workflow . Id ,
105
+ ExecutionPointerId = pointer . Id ,
106
+ ErrorTime = _datetimeProvider . Now . ToUniversalTime ( ) ,
107
+ Message = String . Format ( "Unable to construct step body {0}" , step . BodyType . ToString ( ) )
108
+ } ) ;
109
+ continue ;
110
+ }
107
111
108
- IStepExecutionContext context = new StepExecutionContext ( )
109
- {
110
- Workflow = workflow ,
111
- Step = step ,
112
- PersistenceData = pointer . PersistenceData ,
113
- ExecutionPointer = pointer ,
114
- Item = pointer . ContextItem
115
- } ;
112
+ IStepExecutionContext context = new StepExecutionContext ( )
113
+ {
114
+ Workflow = workflow ,
115
+ Step = step ,
116
+ PersistenceData = pointer . PersistenceData ,
117
+ ExecutionPointer = pointer ,
118
+ Item = pointer . ContextItem
119
+ } ;
116
120
117
- foreach ( var input in step . Inputs )
118
- input . AssignInput ( workflow . Data , body , context ) ;
121
+ foreach ( var input in step . Inputs )
122
+ input . AssignInput ( workflow . Data , body , context ) ;
119
123
120
124
121
- switch ( step . BeforeExecute ( wfResult , context , pointer , body ) )
122
- {
123
- case ExecutionPipelineDirective . Defer :
124
- continue ;
125
- case ExecutionPipelineDirective . EndWorkflow :
126
- workflow . Status = WorkflowStatus . Complete ;
127
- workflow . CompleteTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
128
- continue ;
129
- }
125
+ switch ( step . BeforeExecute ( wfResult , context , pointer , body ) )
126
+ {
127
+ case ExecutionPipelineDirective . Defer :
128
+ continue ;
129
+ case ExecutionPipelineDirective . EndWorkflow :
130
+ workflow . Status = WorkflowStatus . Complete ;
131
+ workflow . CompleteTime = _datetimeProvider . Now . ToUniversalTime ( ) ;
132
+ continue ;
133
+ }
130
134
131
- var result = await body . RunAsync ( context ) ;
135
+ var result = await body . RunAsync ( context ) ;
132
136
133
- if ( result . Proceed )
134
- {
135
- foreach ( var output in step . Outputs )
136
- output . AssignOutput ( workflow . Data , body , context ) ;
137
- }
137
+ if ( result . Proceed )
138
+ {
139
+ foreach ( var output in step . Outputs )
140
+ output . AssignOutput ( workflow . Data , body , context ) ;
141
+ }
138
142
139
- _executionResultProcessor . ProcessExecutionResult ( workflow , def , pointer , step , result , wfResult ) ;
140
- step . AfterExecute ( wfResult , context , result , pointer ) ;
143
+ _executionResultProcessor . ProcessExecutionResult ( workflow , def , pointer , step , result , wfResult ) ;
144
+ step . AfterExecute ( wfResult , context , result , pointer ) ;
145
+ }
141
146
}
142
147
catch ( Exception ex )
143
148
{
0 commit comments