Beispiel #1
0
        public async Task BaseTest()
        {
            var serviceProvider   = BuildServiceProvider();
            var messageDispatcher = new MessageDispatcher(serviceProvider);

            // Register a handler for the message type

            var handler         = new TestMessageHandler();
            var handlerProvider = ContextualProvider.Create(handler);

            messageDispatcher.Register(handlerProvider);

            var handler2 = new TestMessageHandler();

            messageDispatcher.Register(ContextualProvider.Create(handler2));

            var baseHandler = new TestMessageBaseHandler();

            messageDispatcher.Register(ContextualProvider.Create(baseHandler));

            // If the handler (provider) is registered again, the order of handlers shall stay the same.
            messageDispatcher.Register(handlerProvider);

            var message = new TestMessage("x", 123);

            var dispatchResult = await messageDispatcher.DispatchAsync(message, new DispatchValueDictionary(), publish : false, cancellation : default);

            Assert.IsTrue(dispatchResult.IsSuccess);
            Assert.IsNull(handler.Message);             // Handler shall not be called
            Assert.AreEqual(message, handler2.Message); // Handler2 shall be called
            Assert.IsNull(baseHandler.Message);         // BaseHandler shall not be called
        }
Beispiel #2
0
        public async Task PublishTest()
        {
            var serviceProvider   = BuildServiceProvider();
            var messageDispatcher = new MessageDispatcher(serviceProvider);

            // Register a handler for the message type

            var handler = new TestMessageHandler();

            messageDispatcher.Register(ContextualProvider.Create(handler));

            var handler2 = new TestMessageHandler();

            messageDispatcher.Register(ContextualProvider.Create(handler2));

            var baseHandler = new TestMessageBaseHandler();

            messageDispatcher.Register(ContextualProvider.Create(baseHandler));

            var message = new TestMessage("x", 123);

            var dispatchResult = await messageDispatcher.DispatchAsync(message, new DispatchValueDictionary(), publish : true, cancellation : default);

            Assert.IsTrue(dispatchResult.IsSuccess);
            Assert.AreEqual(message, handler.Message);
            Assert.AreEqual(message, handler2.Message);
            Assert.AreEqual(message, baseHandler.Message);
        }
Beispiel #3
0
        public async Task DeregisterTest()
        {
            var serviceProvider   = BuildServiceProvider();
            var messageDispatcher = new MessageDispatcher(serviceProvider);

            // Register a handler for the message type and deregister it to create a typed dispatcher
            var handler      = new TestMessageHandler();
            var registration = messageDispatcher.Register(ContextualProvider.Create(handler));

            var baseHandler = new TestMessageBaseHandler();

            messageDispatcher.Register(ContextualProvider.Create(baseHandler));

            registration.Dispose();
            await registration.Disposal;

            var message = new TestMessage("x", 123);

            var dispatchResult = await messageDispatcher.DispatchAsync(message, new DispatchValueDictionary(), publish : false, cancellation : default);

            Assert.IsTrue(dispatchResult.IsSuccess);
            Assert.IsNull(handler.Message);
            Assert.AreEqual(message, baseHandler.Message);
        }