| | | 1 | | using Elsa.Mediator.Contexts; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Mediator.Middleware.Notification.Contracts; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using Microsoft.Extensions.Logging; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Mediator.Middleware.Notification.Components; |
| | | 9 | | |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | [UsedImplicitly] |
| | 1 | 12 | | public class NotificationHandlerInvokerMiddleware( |
| | 1 | 13 | | NotificationMiddlewareDelegate next, |
| | 1 | 14 | | ILogger<NotificationHandlerInvokerMiddleware> logger) |
| | | 15 | | : INotificationMiddleware |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public async ValueTask InvokeAsync(NotificationContext context) |
| | | 19 | | { |
| | | 20 | | // Find all handlers for the specified notification. |
| | 11316 | 21 | | var notification = context.Notification; |
| | 11316 | 22 | | var notificationType = notification.GetType(); |
| | 11316 | 23 | | var handlerType = typeof(INotificationHandler<>).MakeGenericType(notificationType); |
| | 11316 | 24 | | var serviceProvider = context.ServiceProvider; |
| | 11316 | 25 | | var notificationHandlers = serviceProvider.GetServices<INotificationHandler>(); |
| | 544585 | 26 | | var handlers = notificationHandlers.Where(x => handlerType.IsInstanceOfType(x)).DistinctBy(x => x.GetType()).ToA |
| | 11316 | 27 | | var strategyContext = new NotificationStrategyContext(context, handlers, logger, serviceProvider, context.Cancel |
| | | 28 | | |
| | 11316 | 29 | | await context.NotificationStrategy.PublishAsync(strategyContext); |
| | | 30 | | |
| | | 31 | | // Invoke next middleware. |
| | 11316 | 32 | | await next(context); |
| | 11316 | 33 | | } |
| | | 34 | | } |