'Why RabbitMQ Consumer stops recieving messages after 5-6min?

I am working with RabbitMQ and ASP.NET Core 6. I have created two basic .NET Core 6 web APIs. One for RabbitMQ message publisher and another for RabbitMQ message consumer. RabbitMQ version is 3.9.13, and the Erlang/OTP version is 24.2.1.

The problem is the RabbitMQ Consumer API Stop recieving messages after 5-6min. Why is it so?

Following is the Publish message code.

                var connection = _connectionFactory.CreateConnection();
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "demo-queue", durable: true, exclusive: false, autoDelete: false, arguments: null);

                    string sendUserInfoMessage = JsonConvert.SerializeObject("Hello Worl");
                    var body = Encoding.UTF8.GetBytes(sendUserInfoMessage);

                    IBasicProperties properties = channel.CreateBasicProperties();
                    properties.Persistent = true;
                    properties.DeliveryMode = 2;
                    properties.Headers = new Dictionary<string, object>();
                    properties.Headers.Add("senderUniqueId", "ABC");
                    //properties.Expiration = "60000";

                    channel.ConfirmSelect();

                    channel.BasicPublish(exchange: "",
                                         routingKey: "demo-queue",
                                         basicProperties: properties,
                                         body: body);

And Code for Consuming Message is as below.

public IModel CreateConsumerChannel()
        {
            IModel channel = _connection.CreateModel();

            channel.QueueDeclare(queue: "demo-queue", durable: true, exclusive: false, autoDelete: false, arguments: null);

            var consumer = new EventingBasicConsumer(channel);

            consumer.Received += (model, e) =>
            {
                var eventName = e.RoutingKey;
                var message = Encoding.UTF8.GetString(e.Body.ToArray());
                string message = JsonConvert.DeserializeObject<string>(message);
                channel.BasicAck(e.DeliveryTag, multiple: false);
            };
            channel.BasicConsume(queue: "demo-queue",
                autoAck: false,
                consumer: consumer);

            //channel.CallbackException += (sender, ea) =>
            //{
            //    _consumerChannel.Dispose();
            //    _consumerChannel = CreateConsumerChannel();
            //};

            return channel;
        }


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source