From 95261e9d0054d78af93b5d8317eb75560b80e8cf Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 29 Apr 2024 15:07:37 +0800 Subject: [PATCH] Support a handler to implement both `Local` and `Distribute`. Resolve #19484 --- .../Volo/Abp/EventBus/AbpEventBusModule.cs | 3 +- ...AndDistributeEventHandlerRegister_Tests.cs | 40 +++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/LocalAndDistributeEventHandlerRegister_Tests.cs diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs index 21e663e5e1..ebc04605c6 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs @@ -53,7 +53,8 @@ public class AbpEventBusModule : AbpModule { localHandlers.Add(context.ImplementationType); } - else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) + + if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) { distributedHandlers.Add(context.ImplementationType); } diff --git a/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/LocalAndDistributeEventHandlerRegister_Tests.cs b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/LocalAndDistributeEventHandlerRegister_Tests.cs new file mode 100644 index 0000000000..c6f912ba33 --- /dev/null +++ b/framework/test/Volo.Abp.EventBus.Tests/Volo/Abp/EventBus/LocalAndDistributeEventHandlerRegister_Tests.cs @@ -0,0 +1,40 @@ +using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Shouldly; +using Volo.Abp.DependencyInjection; +using Volo.Abp.EventBus.Distributed; +using Volo.Abp.EventBus.Local; +using Xunit; + +namespace Volo.Abp.EventBus; + +public class LocalAndDistributeEventHandlerRegister_Tests : EventBusTestBase +{ + [Fact] + public void Should_Register_Both_Local_And_Distribute() + { + var localOptions = GetRequiredService>(); + var distributedOptions = GetRequiredService>(); + + localOptions.Value.Handlers.ShouldContain(x => x == typeof(MyEventHandle)); + distributedOptions.Value.Handlers.ShouldContain(x => x == typeof(MyEventHandle)); + } + + class MyEventDate + { + + } + + class MyEventHandle : ILocalEventHandler, IDistributedEventHandler, ITransientDependency + { + Task ILocalEventHandler.HandleEventAsync(MyEventDate eventData) + { + return Task.CompletedTask; + } + + Task IDistributedEventHandler.HandleEventAsync(MyEventDate eventData) + { + return Task.CompletedTask; + } + } +}