Ejemplo n.º 1
0
        private IEnumerable <Task> ProcessEvent(ConsumeResult <string, byte[]> consumeResult)
        {
            var eventName = consumeResult.Topic;
            var message   = Encoding.UTF8.GetString(consumeResult.Message.Value);

            if (_subscriptionManager.IsEventSubscribed(eventName))
            {
                var policy = BuildProcessEventPolicy();
                using (var serviceScope = _serviceProvider.CreateScope())
                {
                    foreach (var handlerType in _subscriptionManager.GetHandlersForEvent(eventName))
                    {
                        var handler = serviceScope.ServiceProvider.GetRequiredService(handlerType);
                        if (handler == null)
                        {
                            continue;
                        }

                        var eventType        = _subscriptionManager.GetEventTypeByName(eventName);
                        var integrationEvent = JsonConvert.DeserializeObject(message, eventType);
                        var concreteType     = typeof(IEventHandler <>).MakeGenericType(eventType);

                        _logger.LogInformation($"Process event \"{eventName}\" with \"{handler.GetType().Name}\"...");
                        yield return((Task)policy.Execute(() => concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent })));
                    }
                }
            }
        }