Exemplo n.º 1
0
        public async Task <bool> Process(BasicDeliverEventArgs eventMessage)
        {
            var eventName = eventMessage.RoutingKey;

            if (!_eventBusSubscriptionManager.HasSubscribedEvent(eventName))
            {
                _logger.LogWarning($"Event '{eventName}' cannot be processed because it is not subscribed");
                return(false);
            }

            var eventType = _eventBusSubscriptionManager.GetEventType(eventName);
            var eventBody = Encoding.UTF8.GetString(eventMessage.Body.ToArray());
            var @event    = JsonConvert.DeserializeObject(eventBody, eventType);

            using (var scope = _lifetimeScope.BeginLifetimeScope())
            {
                var handlerType  = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType);
                var eventHandler = scope.Resolve(handlerType);

                await(Task) handlerType.GetMethod("Handle")?.Invoke(eventHandler, new[] { @event });
            }

            return(true);
        }