示例#1
0
        public IEnumerable <IMessageHandlerInvoker> LoadMessageHandlerInvokers(TypeSource typeSource)
        {
            foreach (var handlerType in typeSource.GetTypes())
            {
                if (!handlerType.IsClass || handlerType.IsAbstract || !handlerType.IsVisible || !_handlerType.IsAssignableFrom(handlerType))
                {
                    continue;
                }

                var subscriptionMode = MessageHandlerInvoker.GetExplicitSubscriptionMode(handlerType);
                var interfaces       = handlerType.GetInterfaces();

                var excludedMessageTypes = interfaces.Where(IsExtendedMessageHandlerInterface)
                                           .Select(handleInterface => handleInterface.GetGenericArguments()[0])
                                           .ToHashSet();

                var handleInterfaces = interfaces.Where(IsMessageHandlerInterface);
                foreach (var handleInterface in handleInterfaces)
                {
                    var messageType = handleInterface.GetGenericArguments()[0];
                    if (excludedMessageTypes.Contains(messageType))
                    {
                        continue;
                    }

                    var shouldBeSubscribedOnStartup = MessageHandlerInvoker.MessageShouldBeSubscribedOnStartup(messageType, subscriptionMode);
                    var invoker = BuildMessageHandlerInvoker(handlerType, messageType, shouldBeSubscribedOnStartup);
                    yield return(invoker);
                }
            }
        }
        internal MessageHandlerAction(Type handlerType, Type messageType, MessageHandlerInvoker invoker)
        {
            if (handlerType == null)
            {
                throw new ArgumentNullException(nameof(handlerType));
            }

            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }

            if (invoker == null)
            {
                throw new ArgumentNullException(nameof(invoker));
            }

            if (messageType.GetTypeInfo().IsValueType)
            {
                throw new ArgumentException(
                          $"The message type {messageType.FullName} is a value type. In order to get away from boxing and unboxing, please do not use value types.",
                          nameof(messageType));
            }

            HandlerType = handlerType;
            MessageType = messageType;
            Invoker     = invoker;
        }
示例#3
0
        public async Task TestMessageQueue()
        {
            var logger = new MockLogger();

            var container = new Container(configuration => configuration.AddRegistry <BusRegistry>());

            var messages = Enumerable.Range(0, 50)
                           .Select(_ => new TestMessage())
                           .Select(msg => new MessageDispatch(msg));

            var queue = new DispatchQueue(logger, 10, "TestQueue");
            var cache = container.GetInstance <IMessageHandlerInvokerCache>();

            Assert.AreEqual(0, TestsMessageDispatchingContext.Get());

            queue.Start();

            foreach (var dispatch in messages)
            {
                var type    = typeof(IMessageHandler <>).MakeGenericType(dispatch.Message.GetType());
                var invoker = new MessageHandlerInvoker(cache, MessageHandlerInvokerMode.Synchronous, type);
                queue.RunOrEnqueue(dispatch, invoker);
            }

            await Task.Delay(1000);

            Assert.AreEqual(100, TestsMessageDispatchingContext.Get());
        }
示例#4
0
        private static IMessageHandlerRegistration CreateMessageHandlerRegistration(
            MessageHandlerActionDescriptor memberDescriptor,
            ImmutableArray <IContextualProvider <IMessageProcessor> > processors)
        {
            var configuration = BuildConfiguration(memberDescriptor);

            return(new MessageHandlerRegistration(
                       memberDescriptor.MessageType,
                       configuration,
                       serviceProvider => MessageHandlerInvoker.CreateInvoker(memberDescriptor, processors, serviceProvider)));
        }
        public MessageHandlerMainDispatcher(ConfigSettings config, 
                                            IBus bus, 
                                            MessageHandlerInvoker invoker)
        {
            this.Logger = NullLogger.Instance;

            _bus = bus;
            _invoker = invoker;
            _config = config;

            _message2HandlerBuilder = new ConcurrentDictionary<Type, Func<IMessageHandler>>();
        }
        public async Task WithTaskResultTest()
        {
            var services        = new ServiceCollection();
            var serviceProvider = services.BuildServiceProvider();
            var messageHandler  = new MessageHandler();

            var method     = typeof(MessageHandler).GetMethod("HandleAsync", new[] { typeof(MessageY) });
            var descriptor = new MessageHandlerActionDescriptor(typeof(MessageX), method);

            var invoker = new MessageHandlerInvoker <MessageY>(messageHandler, descriptor, ImmutableArray <IContextualProvider <IMessageProcessor> > .Empty, serviceProvider);

            var dispatchResult = await invoker.HandleAsync(new MessageY { I = 34 }, new DispatchValueDictionary());

            Assert.IsTrue(dispatchResult.IsSuccess);
        }
        public async Task WithDispatchResultTaskTest()
        {
            var services        = new ServiceCollection();
            var serviceProvider = services.BuildServiceProvider();
            var messageHandler  = new MessageHandler();

            var method     = typeof(MessageHandler).GetMethod("HandleAsync", new[] { typeof(MessageX) });
            var descriptor = new MessageHandlerActionDescriptor(typeof(MessageX), method);

            var invoker = new MessageHandlerInvoker <MessageX>(messageHandler, descriptor, ImmutableArray <IContextualProvider <IMessageProcessor> > .Empty, serviceProvider);

            var dispatchResult = await invoker.HandleAsync(new MessageX(), new DispatchValueDictionary());

            Assert.IsTrue(dispatchResult.IsSuccess);
            Assert.IsInstanceOfType(dispatchResult, typeof(SuccessDispatchResult <int>));
            Assert.AreEqual(42, ((SuccessDispatchResult <int>)dispatchResult).Result);
        }
        internal MessageHandlerAction(Type handlerType, Type messageType, MessageHandlerInvoker invoker)
        {
            if (handlerType == null)
            {
                throw new ArgumentNullException(nameof(handlerType));
            }

            if (messageType == null)
            {
                throw new ArgumentNullException(nameof(messageType));
            }

            if (invoker == null)
            {
                throw new ArgumentNullException(nameof(invoker));
            }

            HandlerType = handlerType;
            MessageType = messageType;
            Invoker     = invoker;
        }