Exemplo n.º 1
0
        private async Task ProcessEvent(string eventName, string message)
        {
            var subscrInfo = _subscriptionManager.GetEventHandlersFor(eventName);

            using (var scope = _serviceProvider.CreateScope())
            {
                foreach (var info in subscrInfo)
                {
                    var handlerInstance = scope.ServiceProvider.GetService(info.HandlerType);
                    if (handlerInstance == null)
                    {
                        continue;
                    }
                    object @event;
                    try
                    {
                        @event = JsonSerializer.Deserialize(message, info.EventType);
                    }
                    catch (Exception ex)
                    {
                        _logger.LogWarning($"Deserialization of event {eventName} threw an exception.");
                        _logger.LogWarning(ex.ToString());
                        continue;
                    }
                    var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(info.EventType);
                    await(Task) concreteType.GetMethod("Handle").Invoke(handlerInstance, new object[] { @event });
                }
            }
        }