/// <summary>
        /// Returns a message to process.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns>
        /// A message to process or null if there are no messages to process
        /// </returns>
        /// <exception cref="ReceiveMessageException">An error occurred while attempting to read messages from the queue</exception>
        public IReceivedMessageInternal ReceiveMessage(IMessageContext context)
        {
            if (_configuration.Options().EnableHoldTransactionUntilMessageCommitted)
            {
                _commitConnection   = (c, b) => _handleMessage.CommitMessage.CommitForTransaction(context);
                _rollbackConnection = (c, b) => _handleMessage.RollbackMessage.RollbackForTransaction(context);
            }

            try
            {
                if (_cancelWork.Tokens.Any(m => m.IsCancellationRequested))
                {
                    return(null);
                }

                if (_configuration.Options().QueueType == QueueTypes.RpcReceive)
                {
                    var rpc = context.Get(_configuration.HeaderNames.StandardHeaders.RpcContext);
                    if (rpc.MessageId == null || !rpc.MessageId.HasValue)
                    {
                        return(null);
                    }
                }

                var connection = GetConnectionAndSetOnContext(context);
                try
                {
                    return(_receiveMessages.GetMessage(context, connection, connection1 => _disposeConnection(connection)));
                }
                finally
                {
                    if (!_configuration.Options().EnableHoldTransactionUntilMessageCommitted)
                    {
                        _disposeConnection(connection);
                    }
                }
            }
            catch (PoisonMessageException exception)
            {
                if (exception.MessageId != null && exception.MessageId.HasValue)
                {
                    context.MessageId = exception.MessageId;
                }
                throw;
            }
            catch (Exception exception)
            {
                throw new ReceiveMessageException("An error occurred while attempting to read messages from the queue",
                                                  exception);
            }
        }
        /// <inheritdoc />
        public IReceivedMessageInternal ReceiveMessage(IMessageContext context)
        {
            if (_configuration.Options().EnableHoldTransactionUntilMessageCommitted)
            {
                _commitConnection   = (c, b) => _handleMessage.CommitMessage.Commit(context);
                _rollbackConnection = (c, b) => _handleMessage.RollbackMessage.Rollback(context);
            }

            try
            {
                if (_cancelWork.Tokens.Any(m => m.IsCancellationRequested))
                {
                    return(null);
                }

                var connection = GetConnectionAndSetOnContext(context);
                try
                {
                    return(_receiveMessages.GetMessage(context, connection, connection1 => _disposeConnection(connection), _configuration.Routes, _configuration.GetUserParameters(), _configuration.GetUserClause()));
                }
                finally
                {
                    if (!_configuration.Options().EnableHoldTransactionUntilMessageCommitted)
                    {
                        _disposeConnection(connection);
                    }
                }
            }
            catch (PoisonMessageException exception)
            {
                if (exception.MessageId != null && exception.MessageId.HasValue)
                {
                    context.SetMessageAndHeaders(exception.MessageId, context.Headers);
                }
                throw;
            }
            catch (Exception exception)
            {
                throw new ReceiveMessageException("An error occurred while attempting to read messages from the queue",
                                                  exception);
            }
        }
 /// <summary>
 /// Returns a message to process.
 /// </summary>
 /// <param name="context">The context.</param>
 /// <returns>
 /// A message to process or null if there are no messages to process
 /// </returns>
 /// <exception cref="ReceiveMessageException">An error occurred while attempting to read messages from the queue</exception>
 public IReceivedMessageInternal ReceiveMessage(IMessageContext context)
 {
     try
     {
         return(ReceiveSharedLogic(context) ? _receiveMessages.GetMessage(context) : null);
     }
     catch (PoisonMessageException exception)
     {
         if (exception.MessageId != null && exception.MessageId.HasValue)
         {
             context.SetMessageAndHeaders(exception.MessageId, context.Headers);
         }
         throw;
     }
     catch (Exception exception)
     {
         throw new ReceiveMessageException("An error occurred while attempting to read messages from the queue",
                                           exception);
     }
 }