简体   繁体   中英

Rabbitmq Fanout Exchange C#

I have a problem about Rabitmq fanout exchange my problem is that I want to a publish subscribe example with rabbitmq and c#.So I created 2 project one them is Publisher and it is that

static void Main(string[] args)
{
      try
      {
          var factory = new ConnectionFactory() { HostName = "localhost" };
          using (var connection = factory.CreateConnection())
              using (var channel = connection.CreateModel())
              {
                  channel.ExchangeDeclare("example.exchange", ExchangeType.Fanout, true, false, null);
                  var message = GetMessage(args);
                  var body = Encoding.UTF8.GetBytes(message);
                  channel.BasicPublish(exchange: "example.exchange", routingKey: "", basicProperties: null, body: body);
                  Console.WriteLine(" [x] Sent {0}", message);
              }
        }
        catch (Exception ex)
        {
            Console.Write($"bir hata oluştu{ex.Message}");
        }
}

private static string GetMessage(string[] args)
{
    return ((args.Length > 0)
           ? string.Join(" ", args)
           : "info: Hello World!");
}

And i have a Consumer it is

    static void Main(string[] args)
    {
        var factory = new ConnectionFactory() { HostName = "localhost" };
        using (var connection = factory.CreateConnection())
        using (var channel = connection.CreateModel())
        {
            channel.ExchangeDeclare(exchange: "example.exchange", 
            type:"fanout",durable:true);

            var queueName = channel.QueueDeclare().QueueName;
            channel.QueueBind(queue: queueName, exchange: "foo.exchange", 
            routingKey: "");

            var consumer = new EventingBasicConsumer(channel);
            consumer.Received += (model, ea) =>
            {
                var body = ea.Body;
                var message = Encoding.UTF8.GetString(body);
                Console.WriteLine(" [x] {0}", message);
            };
            channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);

            Console.ReadLine();
        }
    }

But i can't get messages.Why it could be?

So I find your issue. First of all your exchange declaration in your consumer is wrong.

You declare exchange as "example.exchange" in your publisher :

channel.ExchangeDeclare("example.exchange", ExchangeType.Fanout, true, false, null);

But "foo.exchange" in your consumer :

channel.QueueBind(queue: queueName, exchange: "foo.exchange", routingKey: "");

Change "foo.exchange" to "example.exchange".

For your consumer, I'm able to consume messages with following lines:

public static void Main(string[] args)
{
    ConnectionFactory factory = new ConnectionFactory();
    factory.UserName = "guest";
    factory.Password = "guest";
    factory.HostName = "localhost";
    factory.VirtualHost = "/";

    var connection = factory.CreateConnection();
    var channel = connection.CreateModel();

    var queueName = "test-queue";
    channel.QueueDeclare(queueName, false, false, false, null);
    channel.QueueBind(queueName, "example.exchange", "", null);

    var consumer = new EventingBasicConsumer(channel);
    consumer.Received += (model, ea) =>
    {
        var bodyy = ea.Body;
        var messagee = Encoding.UTF8.GetString(bodyy);
        Console.WriteLine("received [x] {0}", messagee);
    };
    channel.BasicConsume(queue: queueName, autoAck: true, consumer: consumer);

    Console.ReadLine();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM