public IDispatcher RegisterConsumer <T>(IMesssageConsumer <T> consumer) { var consumerType = consumer.GetType(); var messageType = typeof(T); logger.Information("starting: adding consumer {0} for type {1}", consumerType.ToString(), messageType.ToString()); addConsumer(consumer, messageType); addConsumerToDispose(consumer, consumerType.ToString()); logger.Information("finished: adding consumer {0} for type {1}", consumerType.ToString(), messageType.ToString()); return(this); }
private void addConsumerToDispose <T>(IMesssageConsumer <T> consumer, string type) { if (consumerSet.All(i => i != consumer)) { consumerSet.Add(consumer); consumersToDispose.Add(() => { try { logger.Information(string.Format("starting: disposing message consumer, {0}", type)); consumer.Dispose(); logger.Information(string.Format("finished: disposing message consumer, {0}", type)); } catch (Exception ex) { logger.Error("An error occurred when disposing {0}. Error={1}", type, ex.ToString()); } }); } }
private void addConsumer <T>(IMesssageConsumer <T> consumer, Type messageType) { if (!consumers.ContainsKey(messageType)) { consumers.Add(messageType, new List <Action <object> >()); } consumers[messageType].Add(async(object input) => { var json = JsonConvert.SerializeObject(input); try { logger.Information(string.Format("starting: Processing message: {0}", json)); await consumer.ConsumeMessage((T)input); logger.Information(string.Format("finished: Processing message: {0}", json)); } catch (Exception ex) { logger.Error(string.Format("Error when processing message <<{0}>> with error <<{1}>>", json, ex.ToString())); } }); }