diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs index 1eb7689aba..64f375130b 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs @@ -6,7 +6,6 @@ using Microsoft.Extensions.Options; using Volo.Abp.Collections; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Local; -using Volo.Abp.Uow; namespace Volo.Abp.EventBus.Distributed; @@ -16,19 +15,16 @@ public class LocalDistributedEventBus : IDistributedEventBus, ISingletonDependen { private readonly ILocalEventBus _localEventBus; - protected IUnitOfWorkManager UnitOfWorkManager { get; } protected IServiceScopeFactory ServiceScopeFactory { get; } protected AbpDistributedEventBusOptions AbpDistributedEventBusOptions { get; } public LocalDistributedEventBus( ILocalEventBus localEventBus, - IUnitOfWorkManager unitOfWorkManager, IServiceScopeFactory serviceScopeFactory, IOptions distributedEventBusOptions) { _localEventBus = localEventBus; - UnitOfWorkManager = unitOfWorkManager; ServiceScopeFactory = serviceScopeFactory; AbpDistributedEventBusOptions = distributedEventBusOptions.Value; Subscribe(distributedEventBusOptions.Value.Handlers); @@ -126,56 +122,24 @@ public class LocalDistributedEventBus : IDistributedEventBus, ISingletonDependen _localEventBus.UnsubscribeAll(eventType); } - public async Task PublishAsync(TEvent eventData, bool onUnitOfWorkComplete = true) + public Task PublishAsync(TEvent eventData, bool onUnitOfWorkComplete = true) where TEvent : class { - await PublishAsync(typeof(TEvent), eventData, onUnitOfWorkComplete); + return _localEventBus.PublishAsync(eventData, onUnitOfWorkComplete); } - public async Task PublishAsync(Type eventType, object eventData, bool onUnitOfWorkComplete = true) + public Task PublishAsync(Type eventType, object eventData, bool onUnitOfWorkComplete = true) { - if (onUnitOfWorkComplete && UnitOfWorkManager.Current != null) - { - AddToUnitOfWork( - UnitOfWorkManager.Current, - new UnitOfWorkEventRecord(eventType, eventData, EventOrderGenerator.GetNext()) - ); - return; - } - - await _localEventBus.PublishAsync(eventType, eventData, onUnitOfWorkComplete: false); - } - - public async Task PublishAsync(TEvent eventData, bool onUnitOfWorkComplete = true, bool useOutbox = true) - where TEvent : class - { - await PublishAsync(typeof(TEvent), eventData, onUnitOfWorkComplete, useOutbox); + return _localEventBus.PublishAsync(eventType, eventData, onUnitOfWorkComplete); } - - public async Task PublishAsync(Type eventType, object eventData, bool onUnitOfWorkComplete = true, bool useOutbox = true) + + public Task PublishAsync(TEvent eventData, bool onUnitOfWorkComplete = true, bool useOutbox = true) where TEvent : class { - if (onUnitOfWorkComplete && UnitOfWorkManager.Current != null) - { - AddToUnitOfWork( - UnitOfWorkManager.Current, - new UnitOfWorkEventRecord(eventType, eventData, EventOrderGenerator.GetNext(), useOutbox) - ); - return; - } - - if (useOutbox && UnitOfWorkManager.Current != null) - { - UnitOfWorkManager.Current.OnCompleted(async() => { - await _localEventBus.PublishAsync(eventType, eventData, onUnitOfWorkComplete: false); - }); - return; - } - - await _localEventBus.PublishAsync(eventType, eventData, onUnitOfWorkComplete: false); + return _localEventBus.PublishAsync(eventData, onUnitOfWorkComplete); } - protected virtual void AddToUnitOfWork(IUnitOfWork unitOfWork, UnitOfWorkEventRecord eventRecord) + public Task PublishAsync(Type eventType, object eventData, bool onUnitOfWorkComplete = true, bool useOutbox = true) { - unitOfWork.AddOrReplaceDistributedEvent(eventRecord); + return _localEventBus.PublishAsync(eventType, eventData, onUnitOfWorkComplete); } -} +} \ No newline at end of file diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs index 497503680d..c239df38c9 100644 --- a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus_Test.cs @@ -1,9 +1,7 @@ using System; using System.Threading.Tasks; -using Shouldly; using Volo.Abp.Domain.Entities.Events.Distributed; using Volo.Abp.MultiTenancy; -using Volo.Abp.Uow; using Xunit; namespace Volo.Abp.EventBus.Distributed; @@ -51,7 +49,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase public async Task Should_Get_TenantId_From_EventEto_Extra_Property() { var tenantId = Guid.NewGuid(); - + DistributedEventBus.Subscribe(GetRequiredService()); await DistributedEventBus.PublishAsync(new MySimpleEto @@ -61,71 +59,7 @@ public class LocalDistributedEventBus_Test : LocalDistributedEventBusTestBase {"TenantId", tenantId.ToString()} } }); - + Assert.Equal(tenantId, MySimpleDistributedSingleInstanceEventHandler.TenantId); } - - [Fact] - public async Task Event_Should_Published_On_UnitOfWorkComplete() - { - var id = 0; - DistributedEventBus.Subscribe(data => - { - id = data.Value; - return Task.CompletedTask; - }); - - var unitOfWorkManager = GetRequiredService(); - using (var uow = unitOfWorkManager.Begin()) - { - await DistributedEventBus.PublishAsync(new MySimpleEventData(3), onUnitOfWorkComplete: true, useOutbox: false); - } - id.ShouldBe(0); - - using (var uow = unitOfWorkManager.Begin()) - { - await DistributedEventBus.PublishAsync(new MySimpleEventData(3), onUnitOfWorkComplete: true, useOutbox: false); - await uow.CompleteAsync(); - } - id.ShouldBe(3); - - id = 0; - using (var uow = unitOfWorkManager.Begin()) - { - await DistributedEventBus.PublishAsync(new MySimpleEventData(3), onUnitOfWorkComplete: false, useOutbox: false); - } - id.ShouldBe(3); - } - - [Fact] - public async Task Event_Should_Published_On_UnitOfWorkComplete_UseOutbox() - { - var id = 0; - DistributedEventBus.Subscribe(data => - { - id = data.Value; - return Task.CompletedTask; - }); - - var unitOfWorkManager = GetRequiredService(); - using (var uow = unitOfWorkManager.Begin()) - { - await DistributedEventBus.PublishAsync(new MySimpleEventData(3), onUnitOfWorkComplete: false, useOutbox: true); - } - id.ShouldBe(0); - - using (var uow = unitOfWorkManager.Begin()) - { - await DistributedEventBus.PublishAsync(new MySimpleEventData(3), onUnitOfWorkComplete: false, useOutbox: true); - await uow.CompleteAsync(); - } - id.ShouldBe(3); - - id = 0; - using (var uow = unitOfWorkManager.Begin()) - { - await DistributedEventBus.PublishAsync(new MySimpleEventData(3), onUnitOfWorkComplete: false, useOutbox: false); - } - id.ShouldBe(3); - } }