From b7c092cacdb78065ec412ee08e33603f22a2ab8b Mon Sep 17 00:00:00 2001 From: cKey <35512826+colinin@users.noreply.github.com> Date: Mon, 21 Mar 2022 17:32:08 +0800 Subject: [PATCH] feat(notifications): notification to be published at the end of the unit of work --- .../Notifications/INotificationDispatcher.cs | 34 ------------------- .../Internal/NotificationSender.cs | 19 ++++++++++- .../Abp/Notifications/NotificationEto.cs | 2 +- 3 files changed, 19 insertions(+), 36 deletions(-) delete mode 100644 aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDispatcher.cs diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDispatcher.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDispatcher.cs deleted file mode 100644 index 7b3338970..000000000 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/INotificationDispatcher.cs +++ /dev/null @@ -1,34 +0,0 @@ -using System; -using System.Threading.Tasks; - -namespace LINGYUN.Abp.Notifications -{ - /// - /// 通知发送者接口 - /// - [Obsolete("Notification system redesigned, publisher interface deactivated, please use INotificationSender")] - public interface INotificationDispatcher - { - ///// - ///// 发送通知 - ///// - ///// 通知名称 - ///// 数据 - ///// 租户 - ///// 级别 - ///// - //Task DispatchAsync(NotificationName notificationName, NotificationData data, Guid? tenantId = null, - // NotificationSeverity notificationSeverity = NotificationSeverity.Info); - - ///// - ///// 发送通知事件 - ///// - ///// 通知名称 - ///// 数据 - ///// 租户 - ///// 级别 - ///// - //Task DispatchEventAsync(NotificationName notificationName, NotificationData data, Guid? tenantId = null, - // NotificationSeverity notificationSeverity = NotificationSeverity.Info); - } -} diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationSender.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationSender.cs index 365b018e4..1b6c905e9 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationSender.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/Internal/NotificationSender.cs @@ -9,6 +9,7 @@ using System.Linq; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Distributed; +using Volo.Abp.Uow; namespace LINGYUN.Abp.Notifications { @@ -30,16 +31,22 @@ namespace LINGYUN.Abp.Notifications /// Reference to . /// protected IDistributedIdGenerator DistributedIdGenerator { get; } + /// + /// Reference to . + /// + protected IUnitOfWorkManager UnitOfWorkManager { get; } protected AbpNotificationOptions Options { get; } public NotificationSender( IDistributedEventBus distributedEventBus, IDistributedIdGenerator distributedIdGenerator, + IUnitOfWorkManager unitOfWorkManager, IOptions options) { Options = options.Value; DistributedEventBus = distributedEventBus; DistributedIdGenerator = distributedIdGenerator; + UnitOfWorkManager = unitOfWorkManager; Logger = NullLogger.Instance; } @@ -88,7 +95,17 @@ namespace LINGYUN.Abp.Notifications Severity = severity }; - await DistributedEventBus.PublishAsync(eto); + if (UnitOfWorkManager.Current != null) + { + UnitOfWorkManager.Current.OnCompleted(async () => + { + await DistributedEventBus.PublishAsync(eto); + }); + } + else + { + await DistributedEventBus.PublishAsync(eto); + } return eto.Id.ToString(); } diff --git a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationEto.cs b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationEto.cs index e778abcec..fd6bfcb93 100644 --- a/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationEto.cs +++ b/aspnet-core/modules/common/LINGYUN.Abp.Notifications/LINGYUN/Abp/Notifications/NotificationEto.cs @@ -39,7 +39,7 @@ namespace LINGYUN.Abp.Notifications /// 如果指定了用户列表,应该在事件订阅程序中通过此集合过滤订阅用户
/// 如果未指定用户列表,应该在事件订阅程序中过滤所有订阅此通知的用户 /// - public List Users { get; set; } + public List Users { get; set; } = new List(); public NotificationEto() : base() { }