Hey guys. I'm brand new to RabbitMQ and having trouble with the last part of the "Hello World" tutorial in C#
https://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html
I am creating a windows service that is responsible for grabbing messages from a queue, deserializing them, and passing them on to a class that handles logging them.
It does successfully pull the messages and drain the queue, but the Received event is not firing, so I am unable to do any work with the received message.
I know that it's getting the messages because the Web UI tool shows the queue being drained if the service is running.
This is my code... you'll notice it's almost identical to the tutorial example except for where I deserialize the message to my "Transaction" type and pass it to a Repository class.
I added some simple debug lines to write some text to a file to verify that the code in that event handler isn't being called. I have lines just like that in OnStart() and OnStop(), and those are both successfully writing to the txt file, but my "This proves that the Received event is firing" text is not being written.
Please help. What am I doing wrong? Code is below:
private void ReadMessagesFromQueue()
{
var factory = new ConnectionFactory() { HostName = "localhost" };
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
//this will create the queue if it doesn't exist already
channel.QueueDeclare(queue: "transactions",
durable: false,
exclusive: false,
autoDelete: false,
arguments: null);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
//DEBUG CODE write message to a text file just to see if this code is being fired
File.WriteAllText(@"C:\Temp\STOR-1516\serviceTest.txt", "This proves that the Received event is firing.");
//END DEBUG CODE
Transaction transaction = JsonConvert.DeserializeObject<Transaction>(message);
transactionRepository.LogTransaction(transaction);
};
channel.BasicConsume(queue: "transactions",
noAck: true,
consumer: consumer);
}
}
}