private async Task HandleDynamicTypeIntegrationEvent(Message message) { var subscribers = _subscriptions.GetSubscribers(message.Label); if (subscribers.IsNullOrEmpty()) { return; } var data = Encoding.UTF8.GetString(message.Body); if (string.IsNullOrWhiteSpace(data)) { return; } dynamic integrationEvent = JsonConvert.DeserializeObject(data); foreach (var subscriber in subscribers) { // Find handler for the message type var handler = ActivatorUtilities.GetServiceOrCreateInstance(_services, subscriber.HandlerType); if (handler == null) { continue; } if (handler is IDynamicIntegrationEventHandler dynamicHandler) { // Process handler await dynamicHandler.Handle(integrationEvent); } } }
private async Task ProcessAsync(Message message) { if (message == null) { return; } var data = Encoding.UTF8.GetString(message.Body); if (string.IsNullOrWhiteSpace(data)) { return; } var eventName = $"{message.Label}{IntegrationEventSuffix}"; if (!_subscriptions.HasSubscribers(eventName)) { return; } using (var scope = _serviceProvider.CreateScope()) { var subscribers = _subscriptions.GetSubscribers(eventName); foreach (var subscriber in subscribers) { if (subscriber.IsDynamic) { if (scope.ServiceProvider.GetService(subscriber.HandlerType) is IDynamicIntegrationEventHandler handler) { dynamic eventData = JObject.Parse(data); await handler.Handle(eventData).ConfigureAwait(false); } } else { var eventType = _subscriptions.GetEventType(eventName); var integrationEvent = JsonConvert.DeserializeObject(data, eventType); var handler = scope.ServiceProvider.GetService(subscriber.HandlerType); if (handler != null) { var concreteType = typeof(IIntegrationEventHandler <>).MakeGenericType(eventType); await(Task) concreteType.GetMethod("Handle") .Invoke(handler, new object[] { integrationEvent }); } } } } // Complete the message so that it is not received again. await _subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); }