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}");
};
}
}