c# – RabbitMQ Basic Consumer

I am consuming a rabbitMQ queue, but when using non-asynchronous operations, my consumption method only obtains one message, but in rabbit it indicates that they were all consumed? Is the rabbit’s behavior like that? I used asynchronous operators and managed to get all messages from the queue.

    private IEnumerable<ReceivedPaymentViewModel> GetMessageQueue()
    {
        var payments = new List<ReceivedPaymentViewModel>();

        using var channel = _rabbitConnection.CreateChannel();
        channel.QueueDeclare(queue: "PaymentProcessedQueue", durable: false, exclusive: false, autoDelete: false,
            arguments: null);

        var consumer = new EventingBasicConsumer(channel);
            
        consumer.Received += (model, ea) =>
        {
            var body = ea.Body.ToArray();
            var message = Encoding.UTF8.GetString(body);
            var messages = JsonSerializer.Deserialize<ReceivedPaymentViewModel>(message);

            if (messages != null)
                payments.Add(messages);
        };

        channel.BasicConsume(queue: "PaymentProcessedQueue", autoAck: true, consumer: consumer);
            
        channel.Close();

        return payments;
    } 


Get messages in the rabbitMQ queue

Read more here: Source link