| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Microsoft.Extensions.DependencyInjection; |
| | | 3 | | |
| | | 4 | | namespace Elsa.Workflows.Pipelines.ActivityExecution; |
| | | 5 | | |
| | | 6 | | public static class ActivityExecutionMiddlewareExtensions |
| | | 7 | | { |
| | | 8 | | extension(IActivityExecutionPipelineBuilder pipelineBuilder) |
| | | 9 | | { |
| | | 10 | | public IActivityExecutionPipelineBuilder UseMiddleware<[DynamicallyAccessedMembers(DynamicallyAccessedMemberType |
| | | 11 | | { |
| | 2105 | 12 | | var delegateFactory = CreateMiddlewareDelegateFactory<TMiddleware>(pipelineBuilder, args); |
| | 2105 | 13 | | return pipelineBuilder.Use(delegateFactory); |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | public IActivityExecutionPipelineBuilder Insert<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.Publi |
| | | 17 | | { |
| | 0 | 18 | | var delegateFactory = CreateMiddlewareDelegateFactory<TMiddleware>(pipelineBuilder, args); |
| | 0 | 19 | | return pipelineBuilder.Insert(index, delegateFactory); |
| | | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Creates a middleware delegate for the specified middleware component. |
| | | 24 | | /// </summary> |
| | | 25 | | public Func<ActivityMiddlewareDelegate, ActivityMiddlewareDelegate> CreateMiddlewareDelegateFactory<[Dynamically |
| | | 26 | | { |
| | 2105 | 27 | | var middleware = typeof(TMiddleware); |
| | | 28 | | |
| | 2105 | 29 | | return next => |
| | 2105 | 30 | | { |
| | 2105 | 31 | | var invokeMethod = MiddlewareHelpers.GetInvokeMethod(middleware); |
| | 2105 | 32 | | var ctorArgs = new[] |
| | 2105 | 33 | | { |
| | 2105 | 34 | | next |
| | 2105 | 35 | | }.Concat(args).ToArray(); |
| | 2105 | 36 | | var instance = ActivatorUtilities.CreateInstance(pipelineBuilder.ServiceProvider, middleware, ctorArgs); |
| | 2105 | 37 | | return (ActivityMiddlewareDelegate)invokeMethod.CreateDelegate(typeof(ActivityMiddlewareDelegate), insta |
| | 2105 | 38 | | }; |
| | | 39 | | } |
| | | 40 | | } |
| | | 41 | | } |