0

I managed to properly create a sender and the messages to be sent to RabbitMQ, but when setting up the consumer with the method given above, all it does is to consume the message, the richtextbox control is not updated, i also cant see the message in Console. I tried to call the method at form load event and the click button event, same result, message is consumed from server but no return on the interface

        public async Task GetMessages()
        {
            try
            {
                var factory = new ConnectionFactory()
                {
                    HostName = "localhost",
                    UserName = "guest",
                    Password = "guest"
                };
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "administrator_queue", durable: false, exclusive: false, autoDelete: false, arguments: null);

                    var consumer = new AsyncEventingBasicConsumer(channel);
                    channel.BasicConsume(queue: "administrator_queue", autoAck: true, consumer: consumer);
                    consumer.Received += async (eventSender, eventArgs) =>
                    {
                        var message = Encoding.UTF8.GetString(eventArgs.Body.ToArray());
                        var currentDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

                        await Task.Run(() =>
                        {
                            Invoke((MethodInvoker)delegate
                            {
                                richTextBox.AppendText($"{currentDate}: {message}\n");
                            });
                        });

                        Console.WriteLine($"Received message: {currentDate}: {message}");
                    };
                }
            }
jmoreno
  • 12,752
  • 4
  • 60
  • 91
DenT
  • 1

1 Answers1

1

The types are being disposed (by using) before the message is received. Try storing them as member variables instead.

Stephen Cleary
  • 437,863
  • 77
  • 675
  • 810
  • Sorry, I'm pretty new to this. Do u mean just removing the using or creating methods like these.: public void InitializeConnectionFactory() {factory = new ConnectionFactory() { HostName = "localhost", UserName = "guest", Password = "guest"};} public void EstablishConnection() { connection = factory.CreateConnection();} public void CreateChannel() { channel = connection.CreateModel(); } . I also tried to create a separate consumer class, same problem. I think I'm misunderstanding something in this logic. Thanks a lot – DenT Jun 20 '23 at 21:49
  • You could do it that way, yes. – Stephen Cleary Jun 20 '23 at 22:56
  • Tried it, same problem. – DenT Jun 21 '23 at 12:24
  • 1
    Please post a minimal repro with the new code. – Stephen Cleary Jun 21 '23 at 12:41
  • @DenT - really, you can't expect anyone to assist you without providing code to demonstrate the issue. It is a simple thing to create a public git repository. – Luke Bakken Jun 30 '23 at 02:28