using System.Text; using System.Threading.Tasks; using Newtonsoft.Json; using RabbitMQ.Client; namespace CleanArchitecture.Domain.Rabbitmq.Actions; public sealed class SendMessage : IRabbitMqAction { private static readonly JsonSerializerSettings s_serializerSettings = new() { TypeNameHandling = TypeNameHandling.Objects }; private readonly string _exchange; private readonly object _message; private readonly string _routingKey; /// If exchange is empty, this is the name of the queue public SendMessage(string routingKey, string exchange, object message) { _routingKey = routingKey; _exchange = exchange; _message = message; } public async Task Perform(IChannel channel) { var json = JsonConvert.SerializeObject(_message, s_serializerSettings); var content = Encoding.UTF8.GetBytes(json); await channel.BasicPublishAsync( _exchange, _routingKey, content); } }