Ejemplo n.º 1
0
        /// <summary>
        /// Dispatch a message through the pipeline. If the message will be consumed, the accept function
        /// is called to allow the endpoint to acknowledge the message reception if applicable
        /// </summary>
        /// <param name="pipeline">The pipeline instance</param>
        /// <param name="message">The message to dispatch</param>
        /// <param name="acknowledge">The function to call if the message will be consumed by the pipeline</param>
        /// <returns>whether the message was consumed</returns>
        public static bool Dispatch <T>(this IInboundMessagePipeline pipeline, T message, Func <T, bool> acknowledge)
            where T : class
        {
            bool consumed = false;

            using (var bodyStream = new MemoryStream())
            {
                ReceiveContext receiveContext = ReceiveContext.FromBodyStream(bodyStream);
                pipeline.Configure(x => receiveContext.SetBus(x.Bus));
                var context = new ConsumeContext <T>(receiveContext, message);

                using (context.CreateScope())
                {
                    foreach (var consumer in pipeline.Enumerate(context))
                    {
                        if (!acknowledge(message))
                        {
                            return(false);
                        }

                        acknowledge = x => true;

                        consumed = true;

                        consumer(context);
                    }
                }
            }

            return(consumed);
        }