bool HandleCorrelatedMessage(TransportMessage transportMessage, LogicalMessages messages)
        {
            if (transportMessage.CorrelationId == null)
            {
                return(false);
            }

            if (transportMessage.CorrelationId == transportMessage.Id) //to make sure that we don't fire callbacks when doing send locals
            {
                return(false);
            }

            BusAsyncResult busAsyncResult;

            if (!UnicastBus.messageIdToAsyncResultLookup.TryRemove(transportMessage.CorrelationId, out busAsyncResult))
            {
                return(false);
            }

            var statusCode = int.MinValue;

            if (transportMessage.IsControlMessage())
            {
                string errorCode;
                if (transportMessage.Headers.TryGetValue(Headers.ReturnMessageErrorCodeHeader, out errorCode))
                {
                    statusCode = int.Parse(errorCode);
                }
            }

            busAsyncResult.Complete(statusCode, messages.Select(lm => lm.Instance).ToArray());

            return(true);
        }
        public void Invoke(BehaviorContext context, Action next)
        {
            var logicalMessages = new LogicalMessages();

            context.Set(logicalMessages);

            if (SkipDeserialization || UnicastBus.SkipDeserialization)
            {
                return;
            }

            var transportMessage = context.TransportMessage;

            object[] rawMessages;

            try
            {
                rawMessages = Extract(transportMessage);
            }
            catch (Exception exception)
            {
                throw new SerializationException(string.Format("An error occurred while attempting to extract logical messages from transport message {0}", transportMessage), exception);
            }

            if (!transportMessage.IsControlMessage() && !rawMessages.Any())
            {
                log.Warn("Received an empty message - ignoring.");
                return;
            }

            foreach (var rawMessage in rawMessages)
            {
                var messageType = MessageMapper.GetMappedTypeFor(rawMessage.GetType());

                logicalMessages.Add(new LogicalMessage(messageType, rawMessage));
            }

            next();
        }