@@ -33,33 +33,46 @@ public async Task RunPreMiddleware(WorkflowInstance workflow, WorkflowDefinition
33
33
}
34
34
35
35
/// <inheritdoc cref="IWorkflowMiddlewareRunner.RunPostMiddleware"/>
36
- public async Task RunPostMiddleware ( WorkflowInstance workflow , WorkflowDefinition def )
36
+ public Task RunPostMiddleware ( WorkflowInstance workflow , WorkflowDefinition def )
37
37
{
38
- var postMiddleware = _middleware
39
- . Where ( m => m . Phase == WorkflowMiddlewarePhase . PostWorkflow ) ;
40
- try
41
- {
42
- await RunWorkflowMiddleware ( workflow , postMiddleware ) ;
43
- }
44
- catch ( Exception exception )
45
- {
46
- await HandleWorkflowMiddlewareError ( def . OnPostMiddlewareError , exception ) ;
47
- }
38
+ return RunWorkflowMiddlewareWithErrorHandling (
39
+ workflow ,
40
+ WorkflowMiddlewarePhase . PostWorkflow ,
41
+ def . OnPostMiddlewareError ) ;
48
42
}
49
43
50
44
/// <inheritdoc cref="IWorkflowMiddlewareRunner.RunExecuteMiddleware"/>
51
- public async Task RunExecuteMiddleware ( WorkflowInstance workflow , WorkflowDefinition def )
45
+ public Task RunExecuteMiddleware ( WorkflowInstance workflow , WorkflowDefinition def )
52
46
{
53
- var executeMiddleware = _middleware
54
- . Where ( m => m . Phase == WorkflowMiddlewarePhase . ExecuteWorkflow ) ;
47
+ return RunWorkflowMiddlewareWithErrorHandling (
48
+ workflow ,
49
+ WorkflowMiddlewarePhase . ExecuteWorkflow ,
50
+ def . OnExecuteMiddlewareError ) ;
51
+ }
52
+
53
+ public async Task RunWorkflowMiddlewareWithErrorHandling (
54
+ WorkflowInstance workflow ,
55
+ WorkflowMiddlewarePhase phase ,
56
+ Type middlewareErrorType )
57
+ {
58
+ var middleware = _middleware . Where ( m => m . Phase == phase ) ;
55
59
56
60
try
57
61
{
58
- await RunWorkflowMiddleware ( workflow , executeMiddleware ) ;
62
+ await RunWorkflowMiddleware ( workflow , middleware ) ;
59
63
}
60
64
catch ( Exception exception )
61
65
{
62
- await HandleWorkflowMiddlewareError ( def . OnExecuteMiddlewareError , exception ) ;
66
+ var errorHandlerType = middlewareErrorType ?? typeof ( IWorkflowMiddlewareErrorHandler ) ;
67
+
68
+ using ( var scope = _serviceProvider . CreateScope ( ) )
69
+ {
70
+ var typeInstance = scope . ServiceProvider . GetService ( errorHandlerType ) ;
71
+ if ( typeInstance is IWorkflowMiddlewareErrorHandler handler )
72
+ {
73
+ await handler . HandleAsync ( exception ) ;
74
+ }
75
+ }
63
76
}
64
77
}
65
78
@@ -73,19 +86,5 @@ private static Task RunWorkflowMiddleware(
73
86
( previous , middleware ) =>
74
87
( ) => middleware . HandleAsync ( workflow , previous ) ) ( ) ;
75
88
}
76
-
77
- private async Task HandleWorkflowMiddlewareError ( Type middlewareErrorType , Exception exception )
78
- {
79
- var errorHandlerType = middlewareErrorType ?? typeof ( IWorkflowMiddlewareErrorHandler ) ;
80
-
81
- using ( var scope = _serviceProvider . CreateScope ( ) )
82
- {
83
- var typeInstance = scope . ServiceProvider . GetService ( errorHandlerType ) ;
84
- if ( typeInstance is IWorkflowMiddlewareErrorHandler handler )
85
- {
86
- await handler . HandleAsync ( exception ) ;
87
- }
88
- }
89
- }
90
89
}
91
90
}
0 commit comments