Ejemplo n.º 1
0
        private async Task ProcessEvent(string eventName, string message)
        {
            _logger.LogTrace("Processing RabbitMQ event: {EventName}", eventName);

            if (_subsManager.HasSubscriptionsForEvent(eventName))
            {
                using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
                {
                    var subscriptions = _subsManager.GetHandlersForEvent(eventName);
                    foreach (var subscription in subscriptions)
                    {
                        var handler = scope.ResolveOptional(subscription.HandlerType);
                        if (handler == null)
                        {
                            continue;
                        }
                        var eventType    = _subsManager.GetEventTypeByName(eventName);
                        var Event        = JsonConvert.DeserializeObject(message, eventType);
                        var concreteType = typeof(IEventHandler <>).MakeGenericType(eventType);

                        await Task.Yield();

                        await(Task) concreteType.GetMethod("Handler").Invoke(handler, new object[] { Event });
                    }
                }
            }
            else
            {
                _logger.LogWarning("No subscription for RabbitMQ event: {EventName}", eventName);
            }
        }
Ejemplo n.º 2
0
 private async Task ProcessEvent(string eventName, string message)
 {
     if (_subsManager.HasSubscriptionsForEvent(eventName))
     {
         using (var scope = _autofac.BeginLifetimeScope(AUTOFAC_SCOPE_NAME))
         {
             var subscriptions = _subsManager.GetHandlersForEvent(eventName);
             foreach (var subscription in subscriptions)
             {
                 if (subscription.IsDynamic)
                 {
                     //var handler=scope.ResolveOptional(subscription.HandlerType) as ID
                 }
                 else
                 {
                     var eventType        = _subsManager.GetEventTypeByName(eventName);
                     var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
                     var handler          = scope.ResolveOptional(subscription.HandlerType);
                     var concreteType     = typeof(IEventHandler <>).MakeGenericType(eventType);
                     await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent });
                 }
             }
         }
     }
 }
Ejemplo n.º 3
0
        private async Task ProcessEvent(string eventName, string message)
        {
            if (_subscriptionsManager.HasSubscriptionsForEvent(eventName))
            {
                var subscriptionInfos = _subscriptionsManager.GetHandlersForEvent(eventName);
                foreach (var subInfo in subscriptionInfos)
                {
                    var handler = _serviceProvider.GetService(subInfo.HandlerServiceType);

                    if (subInfo.IsDynamic)
                    {
                        dynamic eventData = JsonObject.Parse(message);

                        await(handler  as IDynamicIntegrationEventHandler).Handle(eventData);
                    }
                    else
                    {
                        var eventType        = _subscriptionsManager.GetEventTypeByName(eventName);
                        var integrationEvent = JsonSerializer.DeserializeFromString(message, eventType);
                        var intHandler       = handler as IIntegrationEventHandler;

                        var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType);
                        await(Task) concreteType.GetMethod("Handle").Invoke(intHandler, new[] { integrationEvent });
                    }
                }
            }
        }
Ejemplo n.º 4
0
        private async Task <bool> ProcessEvent(string eventName, string message)
        {
            _logger.LogTrace("Processing RabbitMQ event: {EventName}", eventName);

            if (_subscriptionsManager.HasSubscriptions(eventName))
            {
                using (var scope = _serviceProvider.CreateScope())
                {
                    var subscriptions = _subscriptionsManager.GetHandlersForEvent(eventName);
                    foreach (var subscription in subscriptions)
                    {
                        var handler = scope.ServiceProvider.GetService(subscription.HandlerType);
                        if (handler == null)
                        {
                            continue;
                        }
                        var eventType        = _subscriptionsManager.GetEventTypeByName(eventName);
                        var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
                        var concreteType     = typeof(IEventHandler <>).MakeGenericType(eventType);

                        await Task.Yield();

                        await(Task) concreteType.GetMethod("HandleAsync").Invoke(handler, new object[] { integrationEvent });
                    }
                }
            }
            else
            {
                _logger.LogWarning("No subscription for RabbitMQ event: {EventName}", eventName);

                return(false);
            }

            return(true);
        }
Ejemplo n.º 5
0
        public async Task HandleAsync(IntegrationEvent @event, Type eventType, string eventName)
        {
            IEnumerable <Type> eventHandlers = _subscriptionsManager.GetHandlersForEvent(eventName);

            foreach (Type handlerType in eventHandlers)
            {
                var handler = _serviceProvider.GetService(handlerType);
                if (handler == null)
                {
                    continue;
                }

                await ExecuteHandler(eventType, handler, @event);
            }
        }
Ejemplo n.º 6
0
        private async Task <bool> ProcessEvent(string eventName, string message)
        {
            var processed = false;

            if (subscriptionManager.HasSubscriptionsForEvent(eventName))
            {
                using (var scope = serviceCollection.CreateScope())
                {
                    var subscriptions = subscriptionManager.GetHandlersForEvent(eventName);
                    foreach (var subscription in subscriptions)
                    {
                        if (subscription.IsDynamic)
                        {
                            var handler = scope.ServiceProvider.GetRequiredService(subscription.HandlerType);
                            if (!(handler is IDynamicEventHandler))
                            {
                                Console.WriteLine($"Cannot find EventHandler, type {subscription.HandlerType.Name}");
                                Console.WriteLine();
                            }

                            dynamic eventData = JObject.Parse(message);
                            await((IDynamicEventHandler)handler).Handle(eventData);
                        }
                        else
                        {
                            var eventType        = subscriptionManager.GetEventTypeByName(eventName);
                            var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
                            var handler          = scope.ServiceProvider.GetRequiredService(subscription.HandlerType);
                            var concreteType     = typeof(IEventHandler <>).MakeGenericType(eventType);

                            await(Task) concreteType.GetMethod("Handle").Invoke(handler, new[] { integrationEvent });
                        }
                    }
                }
                processed = true;
            }
            else
            {
                Console.WriteLine("No subscription for RabbitMQ event: {EventName}", eventName);
                Console.WriteLine();
            }

            return(processed);
        }
        public static IEnumerable <SubscriptionInfo> GetHandlersForEvent <T>(this ISubscriptionsManager subscriptionsManager) where T : IntegrationEvent
        {
            var eventName = subscriptionsManager.GetEventKey <T>();

            return(subscriptionsManager.GetHandlersForEvent(eventName));
        }