RSS

Tag Archives: consumer

Using RabbitMQ in C# (III) Consuming messages from RabbitMQ


It can be found that In earlier posts, setup RabbitMQ and sending messages are a piece of cake using RabbitMQ C# client. Let’s take a further step and see how to consume messages from RabbitMQ.

Create a console application, and add below code:

public static void Main()
{
        var factory = new ConnectionFactory() { HostName = "…" };
        using (var connection = factory.CreateConnection())
        {
            using (var channel = connection.CreateModel())
            {
//Assuming you have a queue named “whatever” created already
var consumer = new EventingBasicConsumer(channel); consumer.Received += (model, ea) => { var body = ea.Body; var message = Encoding.Default.GetString(body); Console.WriteLine(" [x] Received {0}", message); }; channel.BasicConsume("whatever", true, consumer); Console.WriteLine(" Press [enter] to exit."); Console.ReadLine(); } } }

That is it! Simple?

Advertisements
 
Leave a comment

Posted by on August 17, 2015 in Dotnet/C#, Programming

 

Tags: , , , , , , , , , , , , ,