コード例 #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);
        }
コード例 #2
0
        public static UnsubscribeAction Connect <TOutput>(this IInboundMessagePipeline pipeline,
                                                          IPipelineSink <IConsumeContext <TOutput> > sink)
            where TOutput : class
        {
            var routerConfigurator = new InboundMessageRouterConfigurator(pipeline);

            return(routerConfigurator.FindOrCreate <TOutput>().Connect(sink));
        }
コード例 #3
0
        InboundPipelineConfigurator(IServiceBus bus)
        {
            _subscriptionEventHandlers = new RegistrationList <ISubscriptionEvent>();
            _bus = bus;

            var router = new MessageRouter <IConsumeContext>();

            _pipeline = new InboundMessagePipeline(router, this);
        }
コード例 #4
0
 /// <summary>
 /// Subscribe a component to the pipeline that handles every message
 /// </summary>
 /// <typeparam name="TComponent"></typeparam>
 /// <param name="pipeline">The pipeline to configure</param>
 /// <param name="instance">The instance that will handle the messages</param>
 /// <returns></returns>
 public static UnsubscribeAction ConnectInstance <TComponent>(this IInboundMessagePipeline pipeline, TComponent instance)
     where TComponent : class
 {
     return(pipeline.Configure(x =>
     {
         IInstanceConnector connector = InstanceConnectorCache.GetInstanceConnector <TComponent>();
         return connector.Connect(x, instance);
     }));
 }
コード例 #5
0
        /// <summary>
        /// Subscribe a component type to the pipeline that is resolved from the container for each message
        /// </summary>
        /// <typeparam name="TComponent"></typeparam>
        /// <param name="pipeline">The pipeline to configure</param>
        /// <returns></returns>
        public static UnsubscribeAction ConnectConsumer <TComponent>(this IInboundMessagePipeline pipeline)
            where TComponent : class, new()
        {
            return(pipeline.Configure(x =>
            {
                var consumerFactory = new DelegateConsumerFactory <TComponent>(() => new TComponent());

                IConsumerConnector connector = ConsumerConnectorCache.GetConsumerConnector <TComponent>();
                return connector.Connect(x, consumerFactory);
            }));
        }
コード例 #6
0
        public static UnsubscribeAction ConnectHandler <TMessage>(this IInboundMessagePipeline pipeline,
                                                                  Action <TMessage> handler,
                                                                  Predicate <TMessage> condition)
            where TMessage : class
        {
            return(pipeline.Configure(x =>
            {
                var connector = new HandlerSubscriptionConnector <TMessage>();

                return connector.Connect(x, HandlerSelector.ForSelectiveHandler(condition, handler));
            }));
        }
コード例 #7
0
        /// <summary>
        /// Subscribe a component type to the pipeline that is resolved from the container for each message
        /// </summary>
        /// <typeparam name="TConsumer"></typeparam>
        /// <param name="pipeline">The pipeline to configure</param>
        /// <param name="consumerFactory"></param>
        /// <returns></returns>
        public static UnsubscribeAction ConnectConsumer <TConsumer>(this IInboundMessagePipeline pipeline,
                                                                    Func <TConsumer> consumerFactory)
            where TConsumer : class
        {
            return(pipeline.Configure(x =>
            {
                var factory = new DelegateConsumerFactory <TConsumer>(consumerFactory);

                IConsumerConnector connector = ConsumerConnectorCache.GetConsumerConnector <TConsumer>();
                return connector.Connect(x, factory);
            }));
        }
コード例 #8
0
        public static UnsubscribeAction ConnectToRouter <TOutput>(this IInboundMessagePipeline pipeline,
                                                                  IPipelineSink <IConsumeContext <TOutput> > sink,
                                                                  Func <UnsubscribeAction> subscribedTo)
            where TOutput : class
        {
            var routerConfigurator = new InboundMessageRouterConfigurator(pipeline);

            MessageRouter <IConsumeContext <TOutput> > router = routerConfigurator.FindOrCreate <TOutput>();

            UnsubscribeAction result = router.Connect(sink);

            UnsubscribeAction remove = subscribedTo();

            return(() => result() && (router.SinkCount == 0) && remove());
        }
コード例 #9
0
 public InboundMessageInterceptorConfigurator(IInboundMessagePipeline sink)
 {
     _sink = sink;
 }
コード例 #10
0
		public InboundMessageInterceptorConfigurator(IInboundMessagePipeline sink)
		{
			_sink = sink;
		}
コード例 #11
0
        public static IEnumerable <IPipelineSink <TMessage> > ShouldHaveSubscriptionFor <TMessage>(this IInboundMessagePipeline pipeline)
            where TMessage : class
        {
            DateTime giveUpAt = DateTime.Now + Timeout;

            while (DateTime.Now < giveUpAt)
            {
                var inspector = new PipelineSinkLocator <TMessage>();

                pipeline.Inspect(inspector);

                if (inspector.Result.Count() > 0)
                {
                    return(inspector.Result);
                }

                Thread.Sleep(20);
            }

            Assert.Fail("A subscription for " + typeof(TMessage).ToFriendlyName() + " was not found on the pipeline");

            return(null);
        }
コード例 #12
0
 /// <summary>
 /// Dispatch a message through the pipeline
 /// </summary>
 /// <param name="pipeline">The pipeline instance</param>
 /// <param name="message">The message to dispatch</param>
 public static bool Dispatch <T>(this IInboundMessagePipeline pipeline, T message)
     where T : class
 {
     return(pipeline.Dispatch(message, x => true));
 }