Ejemplo n.º 1
0
        public void DispatchShouldSupportMultipleHandlers()
        {
            // Arrange.
            var evt = new OrderCancelledEvent();

            var executed = 0;
            var handler1 = new OrderCancelledEventHandler1(() => { executed++; });
            var handler2 = new OrderCancelledEventHandler2(() => { executed++; });

            var map = new MessageHandlerMap();
            map.Add(typeof(OrderCancelledEvent), handler1.GetType());
            map.Add(typeof(OrderCancelledEvent), handler2.GetType());

            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(handler1.GetType())).Returns(handler1);
            A.CallTo(() => container.Resolve(handler2.GetType())).Returns(handler2);

            var dispatcher = new MessageDispatcher(container, map);

            // Act.
            dispatcher.Dispatch("CreateOrder", new Envelope<OrderCancelledEvent>(evt)).Wait();

            // Assert.
            Assert.Equal(2, executed);
        }
Ejemplo n.º 2
0
        public void AddShouldAllowMultipleCommandRegistration()
        {
            // Arrange.
            var map = new MessageHandlerMap();

            // Act.
            map.Add(typeof(CancelOrderCommand), typeof(RemoveOrderCommandHandler));
            map.Add(typeof(DeleteOrderCommand), typeof(RemoveOrderCommandHandler));
            map.Add(typeof(DeleteOrderCommand), typeof(CreateOrderCommand));

            // Assert.
            var registrations = map.GetHandlerTypes();
            Assert.Equal(2, registrations.Count);
            Assert.Equal(typeof(CreateOrderCommand), registrations[typeof(DeleteOrderCommand)][1]);
        }
Ejemplo n.º 3
0
        public void DispatchShouldThrowExceptionIfHandlerNotFound()
        {
            // Arrange.
            var map = new MessageHandlerMap();
            map.Add(typeof(CancelOrderCommand), typeof(RemoveOrderCommandHandler));
            var container = A.Fake<IContainer>();
            var dispatcher = new MessageDispatcher(container, map);

            // Act.
            Assert.Throws<MessageDispatcherException>(() =>
            {
                try
                {
                    dispatcher.Dispatch("CreateOrder", new Envelope<CreateOrderCommand>(new CreateOrderCommand())).Wait();
                }
                catch (AggregateException ex)
                {
                    throw ex.InnerException;
                }
            });
        }
Ejemplo n.º 4
0
        public void DispatchShouldExecuteHandler()
        {
            // Arrange.
            var command = new CancelOrderCommand();

            var executed = false;
            var handler = A.Fake<ICommandHandler<CancelOrderCommand>>();
            A.CallTo(() => handler.Handle(command)).Invokes(c => { executed = true; }).Returns(Task.FromResult(0));
            
            var map = new MessageHandlerMap();
            map.Add(typeof(CancelOrderCommand), handler.GetType());
            
            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(handler.GetType())).Returns(handler);

            var dispatcher = new MessageDispatcher(container, map);

            // Act.
            dispatcher.Dispatch("CreateOrder", new Envelope<CancelOrderCommand>(command)).Wait();

            // Assert.
            A.CallTo(() => handler.Handle(command)).MustHaveHappened(Repeated.Exactly.Once);
            Assert.True(executed);
        }
Ejemplo n.º 5
0
 internal void MessageReceiverStarting(IMessageReceiver receiver, MessageHandlerMap map)
 {
     if (IsEnabled())
         MessageReceiverStarting(receiver.GetType().Name, receiver.Path, map.HandlerTypes.Count, map.HandlerTypes.SelectMany(m => m.Value).Count(), Thread.CurrentThread.ManagedThreadId, GetTask());
 }
Ejemplo n.º 6
0
 public MessageDispatcher(IContainer container, MessageHandlerMap handlerMap)
 {
     _container = container;
     _handlerMap = handlerMap;
 }
Ejemplo n.º 7
0
        /// <summary>
        /// Handle the message.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="brokeredMessage"></param>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        /// <returns></returns>
        private async Task OnHandleMessage(IMessageReceiver receiver, MessageSession session, BrokeredMessage brokeredMessage, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceived(brokeredMessage.ContentType, receiver, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

            // Create new isolated scope.
            using (var scope = Configuration.Container.BeginScope())
            {
                var envelope = await BuildMessage(brokeredMessage)
                               .ConfigureAwait(false);

                // Dispatch the message.
                var dispatcher = new MessageDispatcher(scope, handlerMap);
                await dispatcher.Dispatch(brokeredMessage.ContentType, envelope, session != null?new Session(session, Configuration.Serializer) : null)
                .ConfigureAwait(false);
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Start the session receiver.
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        private void StartSessionReceiver(ISessionMessagePump receiver, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceiverStarting(receiver, handlerMap);

            receiver.StartAsync((session, message) => OnHandleMessage(receiver, session, message, handlerMap), OnError).Wait();
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Start the receiver.
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        private void StartReceiver(IMessagePump receiver, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceiverStarting(receiver, handlerMap);

            receiver.StartAsync(message => OnHandleMessage(receiver, null, message, handlerMap), OnError).Wait();
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Handle the message.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="brokeredMessage"></param>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        /// <returns></returns>
        private async Task OnHandleMessage(IMessageReceiver receiver, MessageSession session, BrokeredMessage brokeredMessage, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceived(brokeredMessage.ContentType, receiver, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

            // Create new isolated scope.
            using (var scope = Configuration.Container.BeginScope())
            {
                var envelope = await BuildMessage(brokeredMessage)
                    .ConfigureAwait(false);

                // Dispatch the message.
                var dispatcher = new MessageDispatcher(scope, handlerMap);
                await dispatcher.Dispatch(brokeredMessage.ContentType, envelope, session != null ? new Session(session, Configuration.Serializer) : null)
                    .ConfigureAwait(false);
            }
        }
Ejemplo n.º 11
0
        /// <summary>
        /// Start the receiver.
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        private void StartReceiver(IMessagePump receiver, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceiverStarting(receiver, handlerMap);

            receiver.StartAsync(message => OnHandleMessage(receiver, null, message, handlerMap), OnError).Wait();
        }
Ejemplo n.º 12
0
        public void DispatchShouldThrowIfHandlerNull()
        {
            // Arrange.
            var evt = new OrderCancelledEvent();
            var map = new MessageHandlerMap();
            map.Add(typeof(OrderCancelledEvent), typeof(OrderCancelledEventHandler1));

            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(typeof(OrderCancelledEventHandler1))).Returns(null);

            // Act.
            Assert.Throws<NullReferenceException>(() =>
            {
                try
                {
                    var dispatcher = new MessageDispatcher(container, map);
                    dispatcher.Dispatch("CreateOrder", new Envelope<OrderCancelledEvent>(evt)).Wait();
                }
                catch (AggregateException ex)
                {
                    throw ex.InnerException;
                }
            });
        }
Ejemplo n.º 13
0
        public void DispatchShouldSupportGiveAccessToSession()
        {
            // Arrange.
            var evt = new OrderCancelledEvent();
            var handler = new SessionOrderCancelledEventHandler();

            var map = new MessageHandlerMap();
            map.Add(typeof(OrderCancelledEvent), handler.GetType());

            var container = A.Fake<IContainer>();
            A.CallTo(() => container.Resolve(handler.GetType())).Returns(handler);

            var dispatcher = new MessageDispatcher(container, map);
            var session = A.Fake<ISession>();

            // Act.
            var envelope = new Envelope<OrderCancelledEvent>(evt);
            dispatcher.Dispatch("CreateOrder", envelope, session).Wait();

            // Assert.
            Assert.Equal(session, handler.Session);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Start the session receiver.
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        private void StartSessionReceiver(ISessionMessagePump receiver, MessageHandlerMap handlerMap)
        {
            MessagingEventSource.Log.MessageReceiverStarting(receiver, handlerMap);

            receiver.StartAsync((session, message) => OnHandleMessage(receiver, session, message, handlerMap), OnError, 
                new ServiceBus.Receive.Session.OnSessionMessageOptions { AutoComplete = false }).Wait();
        }
Ejemplo n.º 15
0
        /// <summary>
        /// Start the session receiver.
        /// </summary>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        private void StartSessionReceiver(ISessionMessagePump receiver, MessageHandlerMap handlerMap)
        {
            MessengerEventSource.Log.MessageReceiverStarting(receiver, handlerMap);

            receiver.StartAsync((session, message) => OnHandleMessage(receiver, session, message, handlerMap), OnError).Wait();
        }
Ejemplo n.º 16
0
        /// <summary>
        /// Handle the message.
        /// </summary>
        /// <param name="session"></param>
        /// <param name="brokeredMessage"></param>
        /// <param name="receiver"></param>
        /// <param name="handlerMap"></param>
        /// <returns></returns>
        private async Task OnHandleMessage(IMessageReceiver receiver, MessageSession session, BrokeredMessage brokeredMessage, MessageHandlerMap handlerMap)
        {
            MessagingEventSource.Log.MessageReceived(brokeredMessage.ContentType, receiver, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

            // Create new isolated scope.
            using (var scope = Configuration.Container.BeginScope())
            {
                var envelope = await BuildMessage(brokeredMessage);

                // Dispatch the message.
                var dispatcher = new MessageDispatcher(scope, handlerMap);
                await dispatcher.Dispatch(brokeredMessage.ContentType, envelope, session != null ? new Session(session, Configuration.Serializer) : null);

                // Cleanup the message.
                var messageReceiver = receiver as IMessagePump;
                if (messageReceiver != null && messageReceiver.Mode == ReceiveMode.PeekLock)
                {
                    // Log.
                    MessagingEventSource.Log.MessageCompleting(brokeredMessage.ContentType, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

                    // Complete the message.
                    brokeredMessage.TryComplete();
                    return;
                }

                // Cleanup the message.
                var sessionMessageReceiver = receiver as ISessionMessagePump;
                if (sessionMessageReceiver != null && sessionMessageReceiver.Mode == ReceiveMode.PeekLock)
                {
                    // Log.
                    MessagingEventSource.Log.MessageCompleting(brokeredMessage.ContentType, brokeredMessage.MessageId, brokeredMessage.CorrelationId, brokeredMessage.SessionId);

                    // Complete the message.
                    brokeredMessage.TryComplete();
                }
            }
        }
Ejemplo n.º 17
0
 public MessageDispatcher(IContainer container, MessageHandlerMap handlerMap)
 {
     _container  = container;
     _handlerMap = handlerMap;
 }