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

17 Aug

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?

Leave a comment

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


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

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: