public void Subscribe(IExchange exchange, string queueName, HandlerManager queueHandlerManager) { //Loop through request and entity types adding queueMessageHandlers to our internal _handlerManager foreach (Type requestType in EntityTypes.EntityRequestAndResponseTypes) { foreach (Type entityType in EntityTypes.EntityBaseTypes) { var requestWithEntityType = requestType.MakeGenericType(entityType); var queueMessageHandlerType = typeof(QueueMessageHandler <>).MakeGenericType(requestWithEntityType); var queueMessageHandlerList = GenericHelpers.InvokeGenericMethod(queueHandlerManager, requestWithEntityType, typeof(IList), "GetHandlersForType", queueName); var list = queueMessageHandlerList as IList; if (list != null && list.Count > 0) { foreach (var item in list) { GenericHelpers.InvokeGenericMethod(_handlerManager, requestWithEntityType, typeof(void), "AddHandler", new object[] { item }); } } } } //Call subscribe internal SubscribeInternal(exchange, queueName); }
private IHandlerRegistration AddHandlers(IHandlerRegistration handlerReg, IQueue queue) { //This is gonna be a bit NASTY //For this queue we need to find all the types we're currently holding handlers for //then add the internal onMessageReceived handler for each type //The reason for all this is calling consume on a queue with existing handlers for types //wipes them out and we have to add all type handlers again - so each time a type is added to be subscribed to //the whole previous set need to be re-added foreach (Type requestType in EntityTypes.EntityRequestAndResponseTypes) { foreach (Type entityType in EntityTypes.EntityBaseTypes) { var requestWithEntityType = requestType.MakeGenericType(entityType); var queueMessageHandlerType = typeof(QueueMessageHandler <>).MakeGenericType(requestWithEntityType); var queueMessageHandlerList = GenericHelpers.InvokeGenericMethod(_handlerManager, requestWithEntityType, typeof(IList), "GetHandlersForType", queue.Name); var list = queueMessageHandlerList as IList; if (list != null && list.Count > 0) { var messageType = typeof(IMessage <>).MakeGenericType(requestWithEntityType); var addParamType = typeof(Action <,>).MakeGenericType(messageType, typeof(MessageReceivedInfo)); var addMethod = GenericHelpers.GetMethodExt(handlerReg.GetType(), "Add", handlerReg.GetType(), addParamType); var addMethodTyped = addMethod.MakeGenericMethod(requestWithEntityType); var onMessageMethod = GenericHelpers.GetMethodExt(this.GetType(), "OnMessageReceived", typeof(void), new Type[] { messageType, typeof(MessageReceivedInfo) }); var onMessageMethodTyped = onMessageMethod.MakeGenericMethod(requestWithEntityType); var action = Delegate.CreateDelegate(addParamType, this, onMessageMethodTyped); handlerReg = (IHandlerRegistration)addMethodTyped.Invoke(handlerReg, new object[] { action }); } } } return(handlerReg); }