туториал 3

This commit is contained in:
Zara28 2024-09-23 22:45:22 +04:00
parent a830cb2198
commit 4c74a16753
2 changed files with 34 additions and 2 deletions

3
.gitignore vendored
View File

@ -22,3 +22,6 @@
/dozorova_alena_lab_4/Worker/.vs
/dozorova_alena_lab_4/Worker/bin
/dozorova_alena_lab_4/Worker/obj
/dozorova_alena_lab_4/EmitLog/bin
/dozorova_alena_lab_4/ReceiveLogs/.vs
/dozorova_alena_lab_4/ReceiveLogs/bin

View File

@ -1,2 +1,31 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");
using System.Text;
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.ExchangeDeclare(exchange: "logs", type: ExchangeType.Fanout);
// declare a server-named queue
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName,
exchange: "logs",
routingKey: string.Empty);
Console.WriteLine(" [*] Waiting for logs.");
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
byte[] body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine($" [x] {message}");
};
channel.BasicConsume(queue: queueName,
autoAck: true,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();