void InitEventConsumer(IMessageConsumer client) { EventHandler <MessageEventArgs> p = async(sender, e) => { using (var scope = _provider.CreateScope()) { try { var scopeProvider = scope.ServiceProvider; var eventHandlers = scopeProvider.GetServices <IDomainEventHandler>(); if (null != eventHandlers && eventHandlers.Any()) { foreach (var handler in eventHandlers) { var handlerType = handler.GetType(); var messageType = e.EventName; var methodInfo = EventHandlerHelper.GetAsyncHandlingMethod(handler, e.EventName); var eventType = EventHandlerHelper.GetEventType(handler, e.EventName); var param = JsonConvert.DeserializeObject(e.Message.ToString(), eventType); await(Task) methodInfo?.Invoke(handler, new[] { param }); } } client.Commit(); } catch (Exception ex) { _logger.LogError($"Error in microservice {this.GetType().FullName}: {ex.Message}"); client.Reject(); } } }; client.OnMessageReceived += p; }