// internal for testing internal async Task DispatchAsync <TNotification>( TNotification notification, TimeSpan?timeout = default) where TNotification : class { try { using var cancellationTokenSource = _cancellationTokenSourceFactory.Create(timeout ?? _options.Value.DispatchTimeout ?? Timeout.InfiniteTimeSpan); using var serviceScope = _serviceScopeFactory.CreateScope(); using var notificationLogScope = MessagingLogMessages.BeginNotificationScope(_logger, notification); using var providedLogScope = (notification is ILogScopeProvider logScopeProvider) ? logScopeProvider.BeginLogScope(_logger) : null; MessagingLogMessages.HandlersInvoking(_logger); foreach (var handler in serviceScope.ServiceProvider.GetServices <INotificationHandler <TNotification> >()) { using var handlerLogScope = MessagingLogMessages.BeginHandlerScope(_logger, handler); try { MessagingLogMessages.HandlerInvoking(_logger); await handler.HandleNotificationAsync(notification, cancellationTokenSource.Token); MessagingLogMessages.HandlerInvoked(_logger); } catch (Exception ex) { MessagingLogMessages.HandlerFailed(_logger, ex); } } MessagingLogMessages.HandlersInvoked(_logger); } catch (Exception ex) { MessagingLogMessages.DispatchFailed(_logger, ex); } }
/// <inheritdoc /> public async Task PublishAsync <TNotification>( TNotification notification, CancellationToken cancellationToken) where TNotification : class { using var notificationLogScope = MessagingLogMessages.BeginNotificationScope(_logger, notification); using var providedLogScope = (notification is ILogScopeProvider logScopeProvider) ? logScopeProvider.BeginLogScope(_logger) : null; MessagingLogMessages.HandlersInvoking(_logger); foreach (var handler in _serviceProvider.GetServices <INotificationHandler <TNotification> >()) { using var handlerLogScope = MessagingLogMessages.BeginHandlerScope(_logger, handler); MessagingLogMessages.HandlerInvoking(_logger); await handler.HandleNotificationAsync(notification, cancellationToken); MessagingLogMessages.HandlerInvoked(_logger); } MessagingLogMessages.HandlersInvoked(_logger); }