Ejemplo n.º 1
0
 public static bool IsHandler(ref MessageHandlerTuple tuple)
 {
     HashSet<Type> messagesTypes = null;
     if (!_handlerMessageTypesCache.TryGetValue(tuple.Handler.GetType(), out messagesTypes))
     {
         messagesTypes = CacheMessagesTypes(tuple.Handler.GetType());
     }
     return messagesTypes.Contains(tuple.Message.GetType());
 }
Ejemplo n.º 2
0
        public void Publish(object message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message", "Message should not be null");
            }

            var handlers = _handlerSource.ResolveHandlersFor(message.GetType());

            var invokedHandlers = 0;

            foreach (var handler in handlers)
            {
                var tuple = new MessageHandlerTuple(message, handler);

                var dispatchedTimes = 0;
                for (int i = 0; i < _dispatchers.Length; i++)
                {
                    var dispatcher = _dispatchers[i];
                    if (dispatcher.TryInvoke(ref tuple))
                    {
                        dispatchedTimes++;
                    }

                    if (dispatchedTimes > 0 && Logger.IsDebugEnabled)
                    {
                        Logger.Debug("Message {0} dispatched to {1} using {2}", message.GetType(), handler.GetType(), dispatcher.GetType());
                        break;
                    }
                }

                if (dispatchedTimes == 0 && !_settings.IgnoreNoDispatcher)
                {
                    var exceptionMessage = string.Format("Dispatcher not found. Message '{0}'", message);
                    throw new InvalidOperationException(exceptionMessage);
                }
                if (dispatchedTimes > 1 && !_settings.IgnoreMultipleDispatcher)
                {
                    var exceptionMessage = string.Format("Message {0} delivered to multiple dispatchers", message);
                    throw new InvalidOperationException(exceptionMessage);
                }

                invokedHandlers++;
            }

            if (invokedHandlers == 0 && Logger.IsWarnEnabled)
            {
                Logger.Warn("Handler not found. Message '{0}'", message.GetType());
            }

            if (invokedHandlers == 0 && !_settings.IgnoreNoHandlers)
            {
                var exceptionMessage = string.Format("Could not find handler for message {0}", message);
                throw new InvalidOperationException(exceptionMessage);
            }
        }
Ejemplo n.º 3
0
 public static void Invoke(ref MessageHandlerTuple tuple)
 {
     Invoker invoker;
     var messageType = tuple.Message.GetType();
     if (!_invokersCache.TryGetValue(messageType, out invoker))
     {
         invoker = BuildAndCacheInvoker(messageType);
     }
     invoker(tuple);
 }
        public void Publish_calls_the_method_on_subscriber()
        {
            var simplePublishWay = new SimpleDispatcher();
            var message = new TestMessage();
            var handler = new TestHandler();
            var tuple = new MessageHandlerTuple(message, handler);

            var result = simplePublishWay.TryInvoke(ref tuple);

            Assert.That(result, Is.True);
            Assert.That(handler.TestMessage, Is.SameAs(message));
        }
        private static void AssertDispatchesCall(SynchronizationContextDispatcher dispatcher)
        {
            var handler = new TestHandler();
            var message = new TestMessage();

            var tuple = new MessageHandlerTuple(message, handler);

            var invoked = dispatcher.TryInvoke(ref tuple);

            handler.Called.WaitOne(1000);

            Assert.That(invoked, Is.True);
            Assert.That(handler.TestMessage, Is.SameAs(message));
        }
        protected override void Invoke(ref MessageHandlerTuple tuple)
        {
            if (SynchronizationContext.Current == _synchronizationContext)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("Direct invoke '{0}' to '{1}'", tuple.Message, tuple.Handler);
                }

                base.Invoke(ref tuple);
            }
            else
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.Debug("SyncContext post '{0}' to '{1}'", tuple.Message, tuple.Handler);
                }
                _synchronizationContext.Post(PostMessage, tuple);
            }
        }
 public bool CanDispatch(ref MessageHandlerTuple tuple)
 {
     return _messageTypes.GetOrAdd(tuple.Message.GetType(), CheckAttributes);
 }
Ejemplo n.º 8
0
 public bool CanDispatch(ref MessageHandlerTuple tuple)
 {
     return _rule(tuple);
 }