1

I wrote these code lines, but I couldn't figure out how I can print some values on console in consumer.receive. This code is working because I checked some values on RabbitMQ CloudAMQP, but I cannot see any changes on console.

Problem is here:

Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()) + " received");

Full code:

 // publisher
 ConnectionFactory factory = new ConnectionFactory();
 factory.Uri = new Uri("amqps://guest:guest@localhost");
 
 using (IConnection connection = factory.CreateConnection())
 using (IModel channel = connection.CreateModel())
 {
      channel.ExchangeDeclare("kuyruk", type: ExchangeType.Fanout);

      for (int i = 1; i <= 100; i++)
      {
         byte [] bytemessage = Encoding.UTF8.GetBytes($"is - {i}");
 
         IBasicProperties properties = channel.CreateBasicProperties();
         properties.Persistent = true;
 
         channel.BasicPublish(exchange: "kuyruk", routingKey: "", basicProperties: properties, body: bytemessage);
     }     
 }
 
 // Consumer
 ConnectionFactory factory = new ConnectionFactory();
 factory.Uri = new Uri("amqps://guest:guest@localhost");
 
 using (IConnection connection = factory.CreateConnection())
 using (IModel channel = connection.CreateModel())
 {
     channel.ExchangeDeclare("kuyruk", type: ExchangeType.Fanout);
 
     // Here consumer İçin Oluşturulacak Kuyruklara Random İsim Oluşturma
     string queueName = channel.QueueDeclare().QueueName;
     channel.QueueBind(queue: queueName, exchange: "kuyruk", routingKey: "");
 
     channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

     EventingBasicConsumer consumer = new EventingBasicConsumer(channel);
     channel.BasicConsume(queueName, false, consumer);
     consumer.Received += (sender, e) =>
     {
         Thread.Sleep(500);
         Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()) + " received");
         channel.BasicAck(e.DeliveryTag, false);
     };

     Console.Read();     
 }
Duhan Uzun
  • 17
  • 4
  • have you put a breakpoint and checked whether you are receiving messages? – Vivek Nuna Dec 27 '22 at 10:58
  • When asking questions, I recommend that you store username and password information with mask values ​​such as XXX. Your username and password in the link sentence are clearly visible – Adem Catamak Dec 27 '22 at 11:05
  • First you publish the messages, then you create a queue. In this case, the queue you are trying to consume is empty. If you create the queue first and then send the messages, you can see the messages coming. – Adem Catamak Dec 27 '22 at 11:09
  • yes, but i dont understand. [link](https://www.hizliresim.com/ozlv7of) – Duhan Uzun Dec 27 '22 at 11:17
  • @AdemCatamak thanks your advice. i cant understand your last comment. Any chance to edit the code? – Duhan Uzun Dec 27 '22 at 11:23

1 Answers1

1

First you have to create the queue. When you publish a message after this point, it will be queued. If you publish the message first, the message will disappear because there is no queue to send the message to.

Another bug in your code is this: you are creating a consumer channel. You define the action to be taken when the message arrives. Then, when your code run into to the publishing process below, you exit the using block which is wrap your consumer channel. For this reason, consumer is closed and cannot consume message.

//consumer

using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;

var factory = new ConnectionFactory
{
    Uri = new Uri("amqp://guest:guest@localhost")
};

using (IConnection connection = factory.CreateConnection())
using (IModel consumeChannel = connection.CreateModel())
{
    consumeChannel.ExchangeDeclare("kuyruk", type: ExchangeType.Fanout);

    //#region Her Consumer İçin Oluşturulacak Kuyruklara Random İsim Oluşturma
    string queueName = consumeChannel.QueueDeclare().QueueName;
    consumeChannel.QueueBind(queue: queueName, exchange: "kuyruk", routingKey: "");
    //#endregion

    consumeChannel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);

    var consumer = new EventingBasicConsumer(consumeChannel);
    consumeChannel.BasicConsume(queueName, false, consumer);
    consumer.Received += (sender, e) =>
                         {
                             Thread.Sleep(500);
                             Console.WriteLine(Encoding.UTF8.GetString(e.Body.ToArray()) + " recieved");
                             consumeChannel.BasicAck(e.DeliveryTag, false);
                         };

    //publisher
    using (IModel publishChannel = connection.CreateModel())
    {
        publishChannel.ExchangeDeclare("kuyruk", type: ExchangeType.Fanout);
        for (int i = 1; i <= 100; i++)
        {
            var str = $"is - {i}";
            byte[] byteMessage = Encoding.UTF8.GetBytes(str);

            IBasicProperties properties = publishChannel.CreateBasicProperties();
            properties.Persistent = true;

            publishChannel.BasicPublish(exchange: "kuyruk", routingKey: "", basicProperties: properties, body: byteMessage);
        }
    }

    Console.Read();
}
Adem Catamak
  • 1,987
  • 2
  • 17
  • 25