private async Task ProcessEvent(string eventName, string message) { if (!_subscriptionManager.HasSubscriptionForEvent(eventName)) { using (var scope = _autofacScope.BeginLifetimeScope(AUTOFAC_SCOPE_NAME)) { var subscriptions = _subscriptionManager.GetHandlersForEvent(eventName); foreach (var sub in subscriptions) { var handler = scope.ResolveOptional(sub.HandlerType); if (sub.IsDynamic) { var dynamicHandler = handler as IDynamicIntegrationEventHandler; dynamic eventData = JObject.Parse(message); await dynamicHandler.Handle(eventData); } else { var eventType = _subscriptionManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); } } } } }
//Process Event to consumer channel (aka whoever is subscribed) 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) { //parse message into Json Object then handle the eventdata } else { var eventType = _subsManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var handler = scope.ResolveOptional(subscription.HandlerType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); } } } } }
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 }))); } } } }
private async Task ProcessEvent(string eventName, string message) { if (_subsManager.HasSubscriptionsForEvent(eventName)) { using (var scope = _scopeFactory.CreateScope()) { var subscriptions = _subsManager.GetHandlersForEvent(eventName); foreach (var subscription in subscriptions) { if (subscription.IsDynamic) { var handler = scope.ServiceProvider.GetService(subscription.HandlerType) as IDynamicIntegrationEventHandler; dynamic eventData = JObject.Parse(message); await handler.Handle(eventData); } else { var eventType = _subsManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var handler = scope.ServiceProvider.GetService(subscription.HandlerType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); } } } } }
private async Task ProcessEvent(string eventName, string message) { logger.LogTrace($"Processing RabbitMQ event: {eventName}"); if (subscriptionManager.HasSubscritptionsForEvent(eventName)) { using (var scope = autofac.BeginLifetimeScope()) { var subscriptions = subscriptionManager.GetHandlersForEvent(eventName); foreach (var subscription in subscriptions) { var handler = scope.ResolveOptional(subscription.HandlerType); if (handler == null) { continue; } var eventType = subscriptionManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var concreteHandlerType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); // TODO: ???? await Task.Yield(); await(Task) concreteHandlerType.GetMethod("Handle") .Invoke(handler, new object[] { integrationEvent }); } } } else { logger.LogWarning($"No subscription for event : {eventName}"); } }
private async Task ProcessEvent(Envelope envelope) { if (envelope is null) { return; } if (!_busSubscriptionManager.HasSubscriptionsForEvent(envelope.Key)) { return; } using (var scope = _autofac.BeginLifetimeScope()) { var subscriptions = _busSubscriptionManager.GetHandlersForEvent(envelope.Key); foreach (var subscription in subscriptions) { var handler = scope.ResolveOptional(subscription.HandlerType); if (handler is null) { continue; } var eventType = _busSubscriptionManager.GetEventTypeByName(envelope.Key); var integrationEvent = JsonConvert.DeserializeObject(envelope.Data?.ToString(), eventType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); // ReSharper disable once PossibleNullReferenceException await(Task) concreteType .GetMethod(nameof(IIntegrationEventHandler <IIntegrationEvent> .Handle)) ?.Invoke(handler, new[] { integrationEvent }); } } }
private async Task <bool> ProcessEvent( string eventName, string message, InMemoryEventBusSubscriptionsManager.SubscriptionInfo subscription) { if (subscription.IsDynamic) { if (!(_serviceProvider.GetRequiredService(subscription.HandlerType) is IDynamicIntegrationEventHandler handler)) { throw new Exception($"Erro ao obter o handler do evento {eventName}"); } dynamic eventData = JObject.Parse(message); return(await handler.Handle(eventData)); } var eventType = _subscriptionManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); using (var scope = _serviceProvider.CreateScope()) { var handler = scope.ServiceProvider.GetRequiredService(subscription.HandlerType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); var result = (Task <bool>)concreteType .GetMethod("Handle") .Invoke(handler, new[] { integrationEvent }); return(await result); } }
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) { if (subscription.IsDynamic) { var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler; if (handler == null) { continue; } dynamic eventData = JObject.Parse(message); await Task.Yield(); await handler.Handle(eventData); } else { var handler = scope.ResolveOptional(subscription.HandlerType); if (handler == null) { continue; } var eventType = _subsManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await Task.Yield(); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); } } } } else { _logger.LogWarning("No subscription for RabbitMQ event: {EventName}", eventName); } }
private async Task ProcessEvent(string eventName, string message) { if (_subsManager.HasSubscriptionsForEvent(eventName)) { var handlertypes = _subsManager.GetHandlersForEvent(eventName); foreach (var handlerType in handlertypes) { var handler = _services.GetService(handlerType); var eventType = _subsManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await Task.Yield(); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); } } }
private async Task <bool> ProcessEvent(string eventName, string message) { var processed = false; if (_subsManager.HasSubscriptionsForEvent(eventName)) { using (var scope = _autofac.BeginLifetimeScope(_autofacScopeName)) { var subscriptions = _subsManager.GetHandlersForEvent(eventName); foreach (var subscription in subscriptions) { if (subscription.IsDynamic) { var handler = scope.ResolveOptional(subscription.HandlerType) as IDynamicIntegrationEventHandler; if (handler == null) { continue; } dynamic eventData = JObject.Parse(message); await handler.Handle(eventData); } else { var handler = scope.ResolveOptional(subscription.HandlerType); if (handler == null) { continue; } var eventType = _subsManager.GetEventTypeByName(eventName); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new[] { integrationEvent }); } } } processed = true; } return(processed); }
private async Task ProcessEvent(string eventName, string message) { if (!_subscriptionManager.HasSubscriptionsForEvent(eventName)) { throw new InvalidOperationException($"No Subscriptions for event {eventName}"); } using (var scope = _serviceProvider.CreateScope()) { var eventType = _subscriptionManager.GetEventTypeByName(eventName); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); var eventHandlerType = _subscriptionManager.GetEventHandlerType(eventName); var handler = scope.ServiceProvider.GetService(eventHandlerType); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); await(Task) concreteType .GetMethod(nameof(IIntegrationEventHandler <IIntegrationEvent> .Handle)) .Invoke(handler, new[] { integrationEvent }); } }
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) { if (subscription.IsDynamic) { //TODO: For Dynamic. Do I even need it??? } else { var handler = scope.ResolveOptional(subscription.HandlerType); if (handler == null) { continue; } var eventType = _subsManager.GetEventTypeByName(eventName); //var jsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; //var integrationEvent = System.Text.Json.JsonSerializer.Deserialize(message, eventType, jsonSerializerOptions); var integrationEvent = JsonConvert.DeserializeObject(message, eventType); var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await Task.Yield(); await(Task) concreteType.GetMethod("Handle").Invoke(handler, new object[] { integrationEvent }); } } } } else { _logger.LogWarning("No subscription for RabbitMQ event: {EventName}", eventName); } }