Created
June 7, 2025 06:09
-
-
Save berkanserbes/fff11838fc703a540e1bcad91cdadb31 to your computer and use it in GitHub Desktop.
csharp-fanout-exchange-consumer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using RabbitMQ.Client; | |
using RabbitMQ.Client.Events; | |
using System.Text; | |
ConnectionFactory factory = new(); | |
factory.Uri = new(<Your CloudAMQP address>); | |
using IConnection connection = await factory.CreateConnectionAsync(); | |
using IChannel channel = await connection.CreateChannelAsync(); | |
await channel.ExchangeDeclareAsync( | |
exchange: "fanout-exchange-example", | |
type: ExchangeType.Fanout | |
); | |
Console.Write("Kuyruk adını giriniz: "); | |
string queueName = Console.ReadLine()!; | |
await channel.QueueDeclareAsync( | |
queue: queueName, | |
exclusive: false | |
); | |
await channel.QueueBindAsync( | |
queue: queueName, | |
exchange: "fanout-exchange-example", | |
routingKey: string.Empty | |
); | |
AsyncEventingBasicConsumer consumer = new(channel); | |
consumer.ReceivedAsync += (sender, e) => | |
{ | |
string message = Encoding.UTF8.GetString(e.Body.Span); | |
Console.WriteLine($"Received message: {message}"); | |
return Task.CompletedTask; | |
}; | |
await channel.BasicConsumeAsync( | |
queue: queueName, | |
autoAck: false, | |
consumer: consumer | |
); | |
Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment