Beispiel #1
0
        private void AttachConsumers()
        {
            IQueue productEntityUpdateQueue = null;

            //Attach to message bus for all o
            foreach (Type type in EntityTypes.ProductEntityTypes)
            {
                //Create needed generic types
                Type entityRequestType  = typeof(EntityChangeRequest <>).MakeGenericType(new Type[] { type });
                Type messageType        = typeof(IMessage <>).MakeGenericType(new Type[] { entityRequestType });
                Type genericMessageType = messageType.GetGenericTypeDefinition();
                Type delegateType       = typeof(ActionDelegate <>).MakeGenericType(new Type[] { type });

                //Create our action delegate (called by the consume method on advanced message bus to process messages)
                MethodInfo consumeMethodInfo = GetType().GetGenericMethod(BindingFlags.Instance | BindingFlags.NonPublic, "OnEntityUpdate", new Type[] { genericMessageType, typeof(MessageReceivedInfo) }).MakeGenericMethod(type);
                Type       actionType        = typeof(Action <,>).MakeGenericType(new Type[] { messageType, typeof(MessageReceivedInfo) });
                Delegate   actionDelegate    = Delegate.CreateDelegate(actionType, this, consumeMethodInfo);

                //Set up call to consume method on advanced message bus
                Type     genericActionType = actionType.GetGenericTypeDefinition();
                Type[]   typeArgs          = new Type[] { typeof(IQueue), genericActionType };
                var      method            = _messageBusAdvanced.GetType().GetGenericMethod("Consume", typeArgs);
                var      methodRef         = method.MakeGenericMethod(entityRequestType);
                object[] parameters        = new object[] { productEntityUpdateQueue, actionDelegate };

                //Invoke consume method
                methodRef.Invoke(_messageBusAdvanced, parameters);
            }
        }