diff --git a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs index d26e489afe..67028207e5 100644 --- a/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.RabbitMQ/Volo/Abp/EventBus/RabbitMq/RabbitMqDistributedEventBus.cs @@ -49,8 +49,6 @@ namespace Volo.Abp.EventBus.RabbitMq HandlerFactories = new ConcurrentDictionary>(); EventTypes = new ConcurrentDictionary(); - - Initialize(); } public void Initialize() @@ -97,7 +95,12 @@ namespace Volo.Abp.EventBus.RabbitMq public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) { var handlerFactories = GetOrCreateHandlerFactories(eventType); - + + if (factory.IsInFactories(handlerFactories)) + { + return NullDisposable.Instance; + } + handlerFactories.Add(factory); if (handlerFactories.Count == 1) //TODO: Multi-threading! 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 3b2da52a96..551b508a40 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.ComponentModel.Design; using System.Linq; using System.Reflection; using System.Runtime.CompilerServices; diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerFactory.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerFactory.cs index 9fbb81b014..279badbca6 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerFactory.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IEventHandlerFactory.cs @@ -1,3 +1,5 @@ +using System.Collections.Generic; + namespace Volo.Abp.EventBus { /// @@ -10,5 +12,7 @@ namespace Volo.Abp.EventBus /// /// The event handler IEventHandlerDisposeWrapper GetHandler(); + + bool IsInFactories(List handlerFactories); } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs index e3e4748ff6..2a028a521e 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; using Microsoft.Extensions.DependencyInjection; using Volo.Abp.DependencyInjection; @@ -33,6 +35,13 @@ namespace Volo.Abp.EventBus ); } + public bool IsInFactories(List handlerFactories) + { + return handlerFactories + .OfType() + .Any(f => f.HandlerType == HandlerType); + } + public void Dispose() { 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 e8786073a7..18470a30fd 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 @@ -49,7 +49,12 @@ namespace Volo.Abp.EventBus.Local { GetOrCreateHandlerFactories(eventType) .Locking(factories => - factories.Add(factory) + { + if (!factory.IsInFactories(factories)) + { + factories.Add(factory); + } + } ); return new EventHandlerFactoryUnregistrar(this, eventType, factory); diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/SingleInstanceHandlerFactory.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/SingleInstanceHandlerFactory.cs index 4fdd9cce69..7e7a69f3cc 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/SingleInstanceHandlerFactory.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/SingleInstanceHandlerFactory.cs @@ -1,3 +1,6 @@ +using System.Collections.Generic; +using System.Linq; + namespace Volo.Abp.EventBus { /// @@ -27,5 +30,12 @@ namespace Volo.Abp.EventBus { return new EventHandlerDisposeWrapper(HandlerInstance); } + + public bool IsInFactories(List handlerFactories) + { + return handlerFactories + .OfType() + .Any(f => f.HandlerInstance == HandlerInstance); + } } } \ No newline at end of file diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/TransientEventHandlerFactory.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/TransientEventHandlerFactory.cs index 6cacc3522d..af5f66c01e 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/TransientEventHandlerFactory.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/TransientEventHandlerFactory.cs @@ -1,4 +1,6 @@ using System; +using System.Collections.Generic; +using System.Linq; namespace Volo.Abp.EventBus { @@ -7,22 +9,62 @@ namespace Volo.Abp.EventBus /// by a transient instance object. /// /// - /// This class always creates a new transient instance of handler. + /// This class always creates a new transient instance of the handler type. /// - public class TransientEventHandlerFactory : IEventHandlerFactory + public class TransientEventHandlerFactory : TransientEventHandlerFactory, IEventHandlerFactory where THandler : IEventHandler, new() { + public TransientEventHandlerFactory() + : base(typeof(THandler)) + { + + } + + protected override IEventHandler CreateHandler() + { + return new THandler(); + } + } + + /// + /// This implementation is used to handle events + /// by a transient instance object. + /// + /// + /// This class always creates a new transient instance of the handler type. + /// + public class TransientEventHandlerFactory : IEventHandlerFactory + { + public Type HandlerType { get; } + + public TransientEventHandlerFactory(Type handlerType) + { + HandlerType = handlerType; + } + /// /// Creates a new instance of the handler object. /// /// The handler object - public IEventHandlerDisposeWrapper GetHandler() + public virtual IEventHandlerDisposeWrapper GetHandler() { - var handler = new THandler(); + var handler = CreateHandler(); return new EventHandlerDisposeWrapper( handler, () => (handler as IDisposable)?.Dispose() ); } + + public bool IsInFactories(List handlerFactories) + { + return handlerFactories + .OfType() + .Any(f => f.HandlerType == HandlerType); + } + + protected virtual IEventHandler CreateHandler() + { + return (IEventHandler) Activator.CreateInstance(HandlerType); + } } } \ No newline at end of file