using api.DTOs; using RabbitMQ.Client; using System.Text; using System.Text.Json; namespace api.RabbitMQ; public class RabbitMQProducer : IMessageProducer { private IChannel? channel; private readonly string personQueueName = "persons"; public async void SendMessage(T message) { if (channel == null) { ConnectionFactory factory = new ConnectionFactory { Uri = new Uri("amqp://guest:guest@localhost:5672") }; IConnection conn = await factory.CreateConnectionAsync(); channel = await conn.CreateChannelAsync(); await channel.QueueDeclareAsync(personQueueName); } if (message is PersonDto) { string jsonString = JsonSerializer.Serialize(message); byte[] body = Encoding.UTF8.GetBytes(jsonString); _ = channel.BasicPublishAsync(exchange: "", routingKey: personQueueName, body: body); } } }