From fd10ddb9d3644c00cbe3a0bf2eb4e750e667f2ef Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Sat, 8 Jan 2022 11:09:57 +0800 Subject: [PATCH] Update EventHandlerInvoker --- .../Volo/Abp/EventBus/EventBusBase.cs | 2 +- .../Volo/Abp/EventBus/EventHandlerInvoker.cs | 17 +++++++++++++---- .../Volo/Abp/EventBus/IEventHandlerInvoker.cs | 5 +++-- .../MyProjectNameModule.cs | 4 +++- 4 files changed, 20 insertions(+), 8 deletions(-) diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs index b6805de1ae..bc307a1777 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs @@ -216,7 +216,7 @@ public abstract class EventBusBase : IEventBus { if (ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(ILocalEventHandler<>)) || ReflectionHelper.IsAssignableToGenericType(handlerType, typeof(IDistributedEventHandler<>))) { - await EventHandlerInvoker.InvokeAsync(eventHandlerWrapper.EventHandler, eventData); + await EventHandlerInvoker.InvokeAsync(eventHandlerWrapper.EventHandler, eventData, eventType); } else { diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs index 986615e134..35d1f4e74c 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Concurrent; +using System.Linq; using System.Reflection; using System.Threading.Tasks; using Volo.Abp.DependencyInjection; @@ -9,18 +10,26 @@ namespace Volo.Abp.EventBus; public class EventHandlerInvoker : IEventHandlerInvoker, ISingletonDependency { private const string EventHandlerMethodName = "HandleEventAsync"; - private readonly ConcurrentDictionary _executorCache; + private readonly ConcurrentDictionary _executorCache; public EventHandlerInvoker() { - _executorCache = new ConcurrentDictionary(); + _executorCache = new ConcurrentDictionary(); } - public Task InvokeAsync(IEventHandler eventHandler, object eventData) + public Task InvokeAsync(IEventHandler eventHandler, object eventData, Type eventType) { var handleType = eventHandler.GetType(); - var executor = _executorCache.GetOrAdd(handleType, type => EventHandlerMethodExecutor.Create(type.GetMethod(EventHandlerMethodName), type.GetTypeInfo())); + var key = $"{handleType.FullName}_{eventType.FullName}"; + + var executor = _executorCache.GetOrAdd(key, _ => EventHandlerMethodExecutor.Create(GetHandleEventMethodInfo(handleType, eventType), handleType.GetTypeInfo())); return executor.ExecuteAsync(eventHandler, new[] { eventData }); } + + private static MethodInfo GetHandleEventMethodInfo(Type handleType, Type eventType) + { + var methods = handleType.GetMethods().Where(x => x.Name == EventHandlerMethodName).ToArray(); + return methods.Length == 1 ? methods.First() : methods.FirstOrDefault(x => x.GetParameters().Any(param => param.ParameterType == eventType)); + } } diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerInvoker.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerInvoker.cs index 37019cce62..71c2bb552c 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerInvoker.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerInvoker.cs @@ -1,8 +1,9 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; namespace Volo.Abp.EventBus; public interface IEventHandlerInvoker { - Task InvokeAsync(IEventHandler eventHandler, object eventData); + Task InvokeAsync(IEventHandler eventHandler, object eventData, Type eventType); } diff --git a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs index c7045a62ea..e2fcf991a6 100644 --- a/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs +++ b/templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs @@ -5,12 +5,14 @@ using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using Volo.Abp; using Volo.Abp.Autofac; +using Volo.Abp.EventBus; using Volo.Abp.Modularity; namespace MyCompanyName.MyProjectName; [DependsOn( - typeof(AbpAutofacModule) + typeof(AbpAutofacModule), + typeof(AbpEventBusModule) )] public class MyProjectNameModule : AbpModule {