Browse Source

Update EventHandlerInvoker

pull/11232/head
liangshiwei 4 years ago
parent
commit
fd10ddb9d3
  1. 2
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs
  2. 17
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs
  3. 5
      framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerInvoker.cs
  4. 4
      templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs

2
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<>))) 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 else
{ {

17
framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventHandlerInvoker.cs

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading.Tasks; using System.Threading.Tasks;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -9,18 +10,26 @@ namespace Volo.Abp.EventBus;
public class EventHandlerInvoker : IEventHandlerInvoker, ISingletonDependency public class EventHandlerInvoker : IEventHandlerInvoker, ISingletonDependency
{ {
private const string EventHandlerMethodName = "HandleEventAsync"; private const string EventHandlerMethodName = "HandleEventAsync";
private readonly ConcurrentDictionary<Type, EventHandlerMethodExecutor> _executorCache; private readonly ConcurrentDictionary<string, EventHandlerMethodExecutor> _executorCache;
public EventHandlerInvoker() public EventHandlerInvoker()
{ {
_executorCache = new ConcurrentDictionary<Type, EventHandlerMethodExecutor>(); _executorCache = new ConcurrentDictionary<string, EventHandlerMethodExecutor>();
} }
public Task InvokeAsync(IEventHandler eventHandler, object eventData) public Task InvokeAsync(IEventHandler eventHandler, object eventData, Type eventType)
{ {
var handleType = eventHandler.GetType(); 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 }); 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));
}
} }

5
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; namespace Volo.Abp.EventBus;
public interface IEventHandlerInvoker public interface IEventHandlerInvoker
{ {
Task InvokeAsync(IEventHandler eventHandler, object eventData); Task InvokeAsync(IEventHandler eventHandler, object eventData, Type eventType);
} }

4
templates/console/src/MyCompanyName.MyProjectName/MyProjectNameModule.cs

@ -5,12 +5,14 @@ using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Volo.Abp; using Volo.Abp;
using Volo.Abp.Autofac; using Volo.Abp.Autofac;
using Volo.Abp.EventBus;
using Volo.Abp.Modularity; using Volo.Abp.Modularity;
namespace MyCompanyName.MyProjectName; namespace MyCompanyName.MyProjectName;
[DependsOn( [DependsOn(
typeof(AbpAutofacModule) typeof(AbpAutofacModule),
typeof(AbpEventBusModule)
)] )]
public class MyProjectNameModule : AbpModule public class MyProjectNameModule : AbpModule
{ {

Loading…
Cancel
Save