0
0
mirror of https://github.com/alex289/CleanArchitecture.git synced 2025-07-01 19:12:57 +00:00
CleanArchitecture/CleanArchitecture.Domain/Rabbitmq/Actions/SendMessage.cs
2023-09-02 10:56:20 +02:00

38 lines
1009 B
C#

using System.Text;
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;
/// <param name="routingKey">If exchange is empty, this is the name of the queue</param>
public SendMessage(string routingKey, string exchange, object message)
{
_routingKey = routingKey;
_exchange = exchange;
_message = message;
}
public void Perform(IModel channel)
{
var json = JsonConvert.SerializeObject(_message, s_serializerSettings);
var content = Encoding.UTF8.GetBytes(json);
channel.BasicPublish(
_exchange,
_routingKey,
null,
content);
}
}