From 318c40a0ee7297d68bb390dbd50c6420d442ee72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?SAL=C4=B0H=20=C3=96ZKARA?= Date: Tue, 10 Mar 2026 14:04:22 +0300 Subject: [PATCH] Get handler types safely and change anon conversion Add GetHandlerType(IEventHandlerFactory) to determine handler Type from known factory implementations (SingleInstance, Ioc, Transient) instead of instantiating handlers. Update LocalEventBus to use this helper when reading LocalEventHandlerOrderAttribute to avoid unnecessary handler creation/disposal. Also switch DistEventScenarioRunner to use AnonymousEventDataConverter.ConvertToLooseObject(...) for anonymous event payload conversion. --- .../Volo/Abp/EventBus/Local/LocalEventBus.cs | 32 +++++++++++++++---- .../DistEventScenarioRunner.cs | 2 +- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs index e8f0715d8d..3281b95bd5 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Local/LocalEventBus.cs @@ -231,11 +231,11 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency { foreach (var factory in handlerFactory.Value) { - using var handler = factory.GetHandler(); + var handlerType = GetHandlerType(factory); handlerFactoryList.Add(new Tuple( factory, handlerFactory.Key, - ReflectionHelper.GetAttributesOfMemberOrDeclaringType(handler.EventHandler.GetType()).FirstOrDefault()?.Order ?? 0)); + ReflectionHelper.GetAttributesOfMemberOrDeclaringType(handlerType).FirstOrDefault()?.Order ?? 0)); } } @@ -243,11 +243,11 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency { foreach (var factory in handlerFactory.Value) { - using var handler = factory.GetHandler(); + var handlerType = GetHandlerType(factory); handlerFactoryList.Add(new Tuple( factory, typeof(AnonymousEventData), - ReflectionHelper.GetAttributesOfMemberOrDeclaringType(handler.EventHandler.GetType()).FirstOrDefault()?.Order ?? 0)); + ReflectionHelper.GetAttributesOfMemberOrDeclaringType(handlerType).FirstOrDefault()?.Order ?? 0)); } } @@ -268,8 +268,7 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency { foreach (var factory in handlerFactory.Value) { - using var handler = factory.GetHandler(); - var handlerType = handler.EventHandler.GetType(); + var handlerType = GetHandlerType(factory); handlerFactoryList.Add(new Tuple( factory, typeof(AnonymousEventData), @@ -330,6 +329,27 @@ public class LocalEventBus : EventBusBase, ILocalEventBus, ISingletonDependency return PublishAsync(typeof(AnonymousEventData), anonymousEventData, onUnitOfWorkComplete); } + private static Type GetHandlerType(IEventHandlerFactory factory) + { + if (factory is SingleInstanceHandlerFactory singleInstanceHandlerFactory) + { + return singleInstanceHandlerFactory.HandlerInstance.GetType(); + } + + if (factory is IocEventHandlerFactory iocEventHandlerFactory) + { + return iocEventHandlerFactory.HandlerType; + } + + if (factory is TransientEventHandlerFactory transientEventHandlerFactory) + { + return transientEventHandlerFactory.HandlerType; + } + + using var handler = factory.GetHandler(); + return handler.EventHandler.GetType(); + } + private bool HasAnonymousHandlers(string eventName) { if (!AnonymousEventHandlerFactories.TryGetValue(eventName, out var handlerFactories)) diff --git a/test/DistEvents/DistDemoApp.Shared/DistEventScenarioRunner.cs b/test/DistEvents/DistDemoApp.Shared/DistEventScenarioRunner.cs index a9601335f8..2bb09937e5 100644 --- a/test/DistEvents/DistDemoApp.Shared/DistEventScenarioRunner.cs +++ b/test/DistEvents/DistDemoApp.Shared/DistEventScenarioRunner.cs @@ -58,7 +58,7 @@ public class DistEventScenarioRunner : IDistEventScenarioRunner, ITransientDepen new SingleInstanceHandlerFactory( new ActionEventHandler(eventData => { - var converted = eventData.ConvertToTypedObject(); + var converted = AnonymousEventDataConverter.ConvertToLooseObject(eventData); if (converted is Dictionary payload && payload.TryGetValue("Message", out var message) && message?.ToString() == profile.AnonymousOnlyMessage)