From 23f4f1e65db57e5192babe9cd68d4888542d8d66 Mon Sep 17 00:00:00 2001 From: Halil ibrahim Kalkan Date: Mon, 19 Nov 2018 15:53:39 +0300 Subject: [PATCH] Disabled distributed event bus. --- .../RabbitMq/RabbitMqDistributedEventBus.cs | 6 ++-- .../Volo/Abp/EventBus/AbpEventBusModule.cs | 9 ++--- .../Distributed/LocalDistributedEventBus.cs | 34 ++++++++++++++++++- .../Volo/Abp/EventBus/EventBusBase.cs | 2 +- .../Abp/EventBus/IocEventHandlerFactory.cs | 16 ++++++--- .../Volo/Abp/EventBus/Local/LocalEventBus.cs | 4 ++- 6 files changed, 57 insertions(+), 14 deletions(-) diff --git a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs index 9f56304959..3cd7cfbe03 100644 --- a/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus.Distributed.RabbitMQ/Volo/Abp/EventBus/Distributed/RabbitMq/RabbitMqDistributedEventBus.cs @@ -37,12 +37,12 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq IConnectionPool connectionPool, IRabbitMqSerializer serializer, IServiceProvider serviceProvider, - DistributedEventBusOptions distributedEventBusOptions) + IOptions distributedEventBusOptions) { ConnectionPool = connectionPool; Serializer = serializer; ServiceProvider = serviceProvider; - DistributedEventBusOptions = distributedEventBusOptions; + DistributedEventBusOptions = distributedEventBusOptions.Value; RabbitMqDistributedEventBusOptions = options.Value; HandlerFactories = new ConcurrentDictionary>(); @@ -52,7 +52,7 @@ namespace Volo.Abp.EventBus.Distributed.RabbitMq Subscribe(DistributedEventBusOptions.Handlers); } - public virtual void Subscribe(ITypeList handlers) + protected virtual void Subscribe(ITypeList handlers) { foreach (var handler in handlers) { 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 7b95c34a3d..f17fd05446 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/AbpEventBusModule.cs @@ -27,10 +27,11 @@ namespace Volo.Abp.EventBus { localHandlers.Add(context.ImplementationType); } - else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) - { - distributedHandlers.Add(context.ImplementationType); - } + //TODO: Distrbiuted event bus is disabled since it's not properly working yet for v0.8 release + //else if (ReflectionHelper.IsAssignableToGenericType(context.ImplementationType, typeof(IDistributedEventHandler<>))) + //{ + // distributedHandlers.Add(context.ImplementationType); + //} }); services.Configure(options => diff --git a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs index ed5b0027cc..30278c6b21 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/Distributed/LocalDistributedEventBus.cs @@ -1,5 +1,8 @@ using System; +using System.Reflection; using System.Threading.Tasks; +using Microsoft.Extensions.Options; +using Volo.Abp.Collections; using Volo.Abp.DependencyInjection; using Volo.Abp.EventBus.Local; @@ -10,10 +13,39 @@ namespace Volo.Abp.EventBus.Distributed public class LocalDistributedEventBus : IDistributedEventBus, ITransientDependency { private readonly ILocalEventBus _localEventBus; + protected IServiceProvider ServiceProvider { get; } + protected DistributedEventBusOptions DistributedEventBusOptions { get; } - public LocalDistributedEventBus(ILocalEventBus localEventBus) + public LocalDistributedEventBus( + ILocalEventBus localEventBus, + IServiceProvider serviceProvider, + IOptions distributedEventBusOptions) { _localEventBus = localEventBus; + ServiceProvider = serviceProvider; + DistributedEventBusOptions = distributedEventBusOptions.Value; + Subscribe(distributedEventBusOptions.Value.Handlers); + } + + public virtual void Subscribe(ITypeList handlers) + { + foreach (var handler in handlers) + { + var interfaces = handler.GetInterfaces(); + foreach (var @interface in interfaces) + { + if (!typeof(IEventHandler).GetTypeInfo().IsAssignableFrom(@interface)) + { + continue; + } + + var genericArgs = @interface.GetGenericArguments(); + if (genericArgs.Length == 1) + { + Subscribe(genericArgs[0], new IocEventHandlerFactory(ServiceProvider, handler)); + } + } + } } public IDisposable Subscribe(Func action) where TEvent : class 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 4c82dbd8eb..e0c0340f15 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/EventBusBase.cs @@ -105,7 +105,7 @@ namespace Volo.Abp.EventBus.Local foreach (var handlerFactories in GetHandlerFactories(eventType)) { - foreach (var handlerFactory in handlerFactories.EventHandlerFactories) + foreach (var handlerFactory in handlerFactories.EventHandlerFactories.ToArray()) //TODO: ToArray should not be needed! { await TriggerHandlerAsync(handlerFactory, handlerFactories.EventType, eventData, exceptions); } 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 a1749e0e48..9d388bae74 100644 --- a/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs +++ b/framework/src/Volo.Abp.EventBus/Volo/Abp/EventBus/IocEventHandlerFactory.cs @@ -7,16 +7,19 @@ namespace Volo.Abp.EventBus /// This implementation is used to get/release /// handlers using Ioc. /// - public class IocEventHandlerFactory : IEventHandlerFactory + public class IocEventHandlerFactory : IEventHandlerFactory, IDisposable { public Type HandlerType { get; } - private readonly IServiceProvider _serviceProvider; + protected IServiceScope ServiceScope { get; } + //TODO: Consider to inject IServiceScopeFactory instead public IocEventHandlerFactory(IServiceProvider serviceProvider, Type handlerType) { - _serviceProvider = serviceProvider; HandlerType = handlerType; + ServiceScope = serviceProvider + .GetRequiredService() + .CreateScope(); } /// @@ -25,11 +28,16 @@ namespace Volo.Abp.EventBus /// Resolved handler object public IEventHandlerDisposeWrapper GetHandler() { - var scope = _serviceProvider.CreateScope(); + var scope = ServiceScope.ServiceProvider.CreateScope(); return new EventHandlerDisposeWrapper( (IEventHandler) scope.ServiceProvider.GetRequiredService(HandlerType), () => scope.Dispose() ); } + + public void Dispose() + { + ServiceScope.Dispose(); + } } } \ No newline at end of file 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 c07e31a642..07a9e609e5 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 @@ -67,7 +67,9 @@ namespace Volo.Abp.EventBus.Local public override IDisposable Subscribe(Type eventType, IEventHandlerFactory factory) { GetOrCreateHandlerFactories(eventType) - .Locking(factories => factories.Add(factory)); + .Locking(factories => + factories.Add(factory) + ); return new EventHandlerFactoryUnregistrar(this, eventType, factory); }