Quantcast
Channel: Programming – Xinyustudio
Viewing all articles
Browse latest Browse all 284

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

$
0
0

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?


Filed under: Dotnet/C#, Programming Tagged: .net, C#, consumer, example, how to, install, installation, publish, Rabbit MQ, RabbitMQ, receive, send, tutorial, tutorials

Viewing all articles
Browse latest Browse all 284

Trending Articles