| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Pipelines.WorkflowExecution; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extensions to <see cref="IWorkflowExecutionPipelineBuilder"/> that adds support for installing <see cref="I |
| | | 8 | | /// </summary> |
| | | 9 | | public static class WorkflowExecutionMiddlewareExtensions |
| | | 10 | | { |
| | | 11 | | extension(IWorkflowExecutionPipelineBuilder pipelineBuilder) |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Installs the specified middleware component into the pipeline being built. |
| | | 15 | | /// </summary> |
| | | 16 | | public IWorkflowExecutionPipelineBuilder UseMiddleware<[DynamicallyAccessedMembers(DynamicallyAccessedMemberType |
| | | 17 | | { |
| | 2125 | 18 | | var delegateFactory = CreateMiddlewareDelegateFactory<TMiddleware>(pipelineBuilder, args); |
| | 2125 | 19 | | return pipelineBuilder.Use(delegateFactory); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Installs the specified middleware component into the pipeline being built. |
| | | 24 | | /// </summary> |
| | | 25 | | public IWorkflowExecutionPipelineBuilder Insert<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Publi |
| | | 26 | | { |
| | 0 | 27 | | var delegateFactory = CreateMiddlewareDelegateFactory<TMiddleware>(pipelineBuilder, args); |
| | 0 | 28 | | return pipelineBuilder.Insert(index, delegateFactory); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <summary> |
| | | 32 | | /// Replaces the terminal middleware component with the specified middleware component. |
| | | 33 | | /// </summary> |
| | | 34 | | public IWorkflowExecutionPipelineBuilder ReplaceTerminal<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTy |
| | | 35 | | { |
| | 4 | 36 | | var index = pipelineBuilder.Components.Count() - 1; |
| | 4 | 37 | | return pipelineBuilder.Replace<TMiddleware>(index, args); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Replaces the middleware component at the specified index with the specified middleware component. |
| | | 42 | | /// </summary> |
| | | 43 | | public IWorkflowExecutionPipelineBuilder Replace<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Publ |
| | | 44 | | { |
| | 4 | 45 | | var delegateFactory = CreateMiddlewareDelegateFactory<TMiddleware>(pipelineBuilder, args); |
| | 4 | 46 | | return pipelineBuilder.Replace(index, delegateFactory); |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Creates a middleware delegate for the specified middleware component. |
| | | 51 | | /// </summary> |
| | | 52 | | public Func<WorkflowMiddlewareDelegate, WorkflowMiddlewareDelegate> CreateMiddlewareDelegateFactory<[Dynamically |
| | | 53 | | { |
| | 2129 | 54 | | var middleware = typeof(TMiddleware); |
| | | 55 | | |
| | 2129 | 56 | | return next => |
| | 2129 | 57 | | { |
| | 2125 | 58 | | var invokeMethod = MiddlewareHelpers.GetInvokeMethod(middleware); |
| | 2125 | 59 | | var ctorParams = new[] |
| | 2125 | 60 | | { |
| | 2125 | 61 | | next |
| | 2125 | 62 | | }.Concat(args).ToArray(); |
| | 2125 | 63 | | var instance = ActivatorUtilities.CreateInstance(pipelineBuilder.ServiceProvider, middleware, ctorParams |
| | 2125 | 64 | | return (WorkflowMiddlewareDelegate)invokeMethod.CreateDelegate(typeof(WorkflowMiddlewareDelegate), insta |
| | 2129 | 65 | | }; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | } |