public void Execute(OnReceiveMessage pipelineEvent)
        {
            var parser = pipelineEvent.Pipeline.State.Get <MsmqUriParser>();
            var tx     = pipelineEvent.Pipeline.State.Get <MessageQueueTransaction>();

            try
            {
                pipelineEvent.Pipeline.State.Add(
                    pipelineEvent.Pipeline.State.Get <MessageQueue>("queue")
                    .Receive(pipelineEvent.Pipeline.State.Get <TimeSpan>("timeout"), tx));
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    pipelineEvent.Pipeline.State.Add <Message>(null);
                    return;
                }

                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.AccessDenied)
                {
                    MsmqQueue.AccessDenied(_log, parser.Path);
                }

                _log.Error(string.Format(MsmqResources.GetMessageError, parser.Uri, ex.Message));

                throw;
            }
        }
        public void Execute(OnReturnJournalMessages pipelineEvent)
        {
            var parser       = pipelineEvent.Pipeline.State.Get <MsmqUriParser>();
            var tx           = pipelineEvent.Pipeline.State.Get <MessageQueueTransaction>();
            var queue        = pipelineEvent.Pipeline.State.Get <MessageQueue>("queue");
            var journalQueue = pipelineEvent.Pipeline.State.Get <MessageQueue>("journalQueue");
            var timeout      = pipelineEvent.Pipeline.State.Get <TimeSpan>("timeout");
            var done         = false;

            try
            {
                while (!done)
                {
                    var journalMessage = DequeueJournalMessage(tx, journalQueue, timeout);

                    if (journalMessage != null)
                    {
                        var message = new Message
                        {
                            Recoverable        = true,
                            UseDeadLetterQueue = parser.UseDeadLetterQueue,
                            Label         = journalMessage.Label,
                            CorrelationId = string.Format(@"{0}\1", journalMessage.Label),
                            BodyStream    = journalMessage.BodyStream.Copy()
                        };

                        queue.Send(message, tx);
                    }
                    else
                    {
                        done = true;
                    }
                }
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.AccessDenied)
                {
                    MsmqQueue.AccessDenied(_log, parser.Path);
                }

                _log.Error(string.Format(Resources.GetMessageError, parser.Path, ex.Message));

                throw;
            }
        }
        public void Execute(OnReleaseMessage pipelineEvent)
        {
            var parser       = pipelineEvent.Pipeline.State.Get <MsmqUriParser>();
            var tx           = pipelineEvent.Pipeline.State.Get <MessageQueueTransaction>();
            var queue        = pipelineEvent.Pipeline.State.Get <MessageQueue>("queue");
            var journalQueue = pipelineEvent.Pipeline.State.Get <MessageQueue>("journalQueue");
            var timeout      = pipelineEvent.Pipeline.State.Get <TimeSpan>("timeout");

            try
            {
                var journalMessage = ReceiveMessage(pipelineEvent.Pipeline.State.Get <Guid>("messageId"), tx,
                                                    journalQueue, timeout);

                if (journalMessage == null)
                {
                    return;
                }

                var message = new Message
                {
                    Recoverable        = true,
                    UseDeadLetterQueue = parser.UseDeadLetterQueue,
                    Label         = journalMessage.Label,
                    CorrelationId = $@"{journalMessage.Label}\1",
                    BodyStream    = journalMessage.BodyStream.Copy()
                };

                queue.Send(message, tx);
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.AccessDenied)
                {
                    MsmqQueue.AccessDenied(_log, parser.Path);
                }

                _log.Error(string.Format(Resources.GetMessageError, parser.Path, ex.Message));

                throw;
            }
        }
        private Message DequeueJournalMessage(MessageQueueTransaction tx, MessageQueue journalQueue, TimeSpan timeout)
        {
            try
            {
                return(journalQueue.Receive(timeout, tx));
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    return(null);
                }

                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.AccessDenied)
                {
                    MsmqQueue.AccessDenied(_log, journalQueue.Path);
                }

                _log.Error(string.Format(Resources.GetMessageError, journalQueue.Path, ex.Message));

                throw;
            }
        }
        private Message ReceiveMessage(Guid messageId, MessageQueueTransaction tx, MessageQueue journalQueue, TimeSpan timeout)
        {
            try
            {
                return(journalQueue.ReceiveByCorrelationId(string.Format(@"{0}\1", messageId), timeout, tx));
            }
            catch (MessageQueueException ex)
            {
                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.IOTimeout)
                {
                    return(null);
                }

                if (ex.MessageQueueErrorCode == MessageQueueErrorCode.AccessDenied)
                {
                    MsmqQueue.AccessDenied(_log, journalQueue.Path);
                }

                _log.Error(string.Format(MsmqResources.GetMessageError, journalQueue.Path, ex.Message));

                throw;
            }
        }