| | 1 | | using Microsoft.Extensions.DependencyInjection; |
| | 2 | | using Microsoft.Extensions.Logging; |
| | 3 | |
|
| | 4 | | namespace MediatR; |
| | 5 | |
|
| | 6 | | /// <summary> |
| | 7 | | /// Extended mediator implementation. |
| | 8 | | /// </summary> |
| | 9 | | /// <param name="serviceScopeFactory"></param> |
| | 10 | | /// <param name="serviceProvider"></param> |
| | 11 | | public class ExtendedMediator( |
| | 12 | | IServiceScopeFactory serviceScopeFactory, |
| | 13 | | IServiceProvider serviceProvider) |
| 15 | 14 | | : Mediator(serviceProvider) |
| | 15 | | { |
| 15 | 16 | | private readonly IServiceScopeFactory _serviceScopeFactory = serviceScopeFactory; |
| 15 | 17 | | private readonly IServiceProvider _serviceProvider = serviceProvider; |
| | 18 | |
|
| 1 | 19 | | private static readonly Dictionary<int, INotificationPublisher> _publishers = new() |
| 1 | 20 | | { |
| 1 | 21 | | [(int)PublishStrategy.Sequential] = new SequentialPublisher(), |
| 1 | 22 | | [(int)PublishStrategy.SequentialAll] = new SequentialAllPublisher(), |
| 1 | 23 | | [(int)PublishStrategy.WhenAll] = new WhenAllPublisher(), |
| 1 | 24 | | }; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Asynchronously send a notification to multiple handlers using the specified strategy. |
| | 28 | | /// </summary> |
| | 29 | | /// <typeparam name="TNotification"></typeparam> |
| | 30 | | /// <param name="notification">Notification object</param> |
| | 31 | | /// <param name="strategy">Publish strategy</param> |
| | 32 | | /// <param name="cancellationToken">Optional cancellation token</param> |
| | 33 | | /// <returns>A task that represents the publish operation.</returns> |
| | 34 | | public Task Publish<TNotification>( |
| | 35 | | TNotification notification, |
| | 36 | | PublishStrategy strategy, |
| | 37 | | CancellationToken cancellationToken = default) |
| | 38 | | where TNotification : INotification |
| | 39 | | { |
| 15 | 40 | | var isBackgroundTask = (int)strategy > 10; |
| 15 | 41 | | var key = isBackgroundTask |
| 15 | 42 | | ? (int)strategy - 10 |
| 15 | 43 | | : (int)strategy; |
| | 44 | |
|
| 15 | 45 | | if (_publishers.TryGetValue(key, out var publisher)) |
| | 46 | | { |
| 14 | 47 | | return isBackgroundTask |
| 14 | 48 | | ? PublishBackground(_serviceScopeFactory, notification, publisher, cancellationToken) |
| 14 | 49 | | : Publish(_serviceProvider, notification, publisher, cancellationToken); |
| | 50 | | } |
| | 51 | |
|
| 1 | 52 | | return Publish(notification, cancellationToken); |
| | 53 | | } |
| | 54 | |
|
| | 55 | | private static Task Publish<TNotification>( |
| | 56 | | IServiceProvider serviceProvider, |
| | 57 | | TNotification notification, |
| | 58 | | INotificationPublisher publisher, |
| | 59 | | CancellationToken cancellationToken) where TNotification : INotification |
| 12 | 60 | | => new Mediator(serviceProvider, publisher).Publish(notification, cancellationToken); |
| | 61 | |
|
| | 62 | | private static Task PublishBackground<TNotification>( |
| | 63 | | IServiceScopeFactory serviceScopeFactory, |
| | 64 | | TNotification notification, |
| | 65 | | INotificationPublisher publisher, |
| | 66 | | CancellationToken cancellationToken) where TNotification : INotification |
| | 67 | | { |
| 2 | 68 | | _ = Task.Run(async () => |
| 2 | 69 | | { |
| 2 | 70 | | using var scope = serviceScopeFactory.CreateScope(); |
| 2 | 71 | | var logger = scope.ServiceProvider.GetService<ILogger<ExtendedMediator>>(); |
| 2 | 72 | |
|
| 2 | 73 | | try |
| 2 | 74 | | { |
| 2 | 75 | | var mediator = new Mediator(scope.ServiceProvider, publisher); |
| 2 | 76 | | await mediator.Publish(notification, cancellationToken).ConfigureAwait(false); |
| 1 | 77 | | } |
| 1 | 78 | | catch (Exception ex) |
| 2 | 79 | | { |
| 2 | 80 | | // The aggregate exceptions are already flattened by the publishers. |
| 1 | 81 | | logger?.LogError(ex, "Error occurred while executing the handler(s) in a background thread!"); |
| 1 | 82 | | } |
| 2 | 83 | |
|
| 4 | 84 | | }, cancellationToken); |
| | 85 | |
|
| 2 | 86 | | return Task.CompletedTask; |
| | 87 | | } |
| | 88 | | } |