Example #1
0
        public void SendToHandler(string id, IEnvelope envelope)
        {
            IDispatchMessage dispatcher;

            if (Dispatchers.TryGetValue(envelope.MessageType, out dispatcher))
            {
                dispatcher.Dispatch(envelope);
            }
            else
            {
                IEnumerable <Type> inheritanceChain = Reflector.GetInheritanceChain(envelope.MessageType);
                Type key = null;
                if ((key = Dispatchers.Keys.FirstOrDefault(k => inheritanceChain.Contains(k))) != null)
                {
                    dispatcher = Dispatchers[key];
                    Dispatchers.AddOrUpdate(envelope.MessageType, x => dispatcher, (x, y) => dispatcher);
                    dispatcher.Dispatch(envelope);
                }
                // message is a response to a request message
                else if (ResponseDispatchers.TryRemove(envelope.CorrelationId, out dispatcher))
                {
                    dispatcher.Dispatch(envelope);
                }
            }
        }
Example #2
0
        //public void Send<TMessage>( IEnvelope<TMessage> envelope )
        //{
        //    Count++;
        //    Fibers.Send(
        //        string.IsNullOrEmpty(
        //            envelope.CorrelationId )
        //            ? (envelope.MessageId.GetHashCode() % 100).ToString()
        //            : envelope.CorrelationId,
        //        envelope );
        //}

        //public void Send( IEnvelope envelope )
        //{
        //    Count++;
        //    Fibers.Send(
        //        string.IsNullOrEmpty(
        //            envelope.CorrelationId )
        //            ? (envelope.MessageId.GetHashCode() % 100).ToString()
        //            : envelope.CorrelationId,
        //        envelope );
        //}

        public void ExpectResponse <TResponse>(string correlationId, Action <TResponse> onResponse)
        {
            ResponseDispatchers.TryAdd(correlationId, new ResponseDispatcher <TResponse>(onResponse));
        }