コード例 #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
        }
コード例 #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);
        }
コード例 #3
0
            public async Task <IHandlerRegistration> RegisterAsync(IContextualProvider <IMessageHandler <TMessage> > handlerProvider)
            {
                var provider = new ContextualProvider <IMessageReplyer <TMessage> >(
                    serviceProvider => new MessageReplyer <TMessage>(_endPoint, handlerProvider.ProvideInstance(serviceProvider)));

                return(await HandlerRegistration.CreateRegistrationAsync(_handler, provider));
            }
コード例 #4
0
 private static void AddMessageProcessors(IServiceCollection services)
 {
     services.Configure <MessagingOptions>(options =>
     {
         options.MessageProcessors.Add(ContextualProvider.Create <EntityMessageHandlerProcessor>());
     });
 }
コード例 #5
0
        public static Task <IHandlerRegistration <IEventHandler <TEvent> > > RegisterAsync <TEvent>(this IEventDispatcher eventDispatcher, IEventHandler <TEvent> eventHandler)
        {
            if (eventDispatcher == null)
            {
                throw new ArgumentNullException(nameof(eventDispatcher));
            }

            return(eventDispatcher.RegisterAsync(ContextualProvider.FromValue(eventHandler)));
        }
コード例 #6
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);
        }