I’m going to publish some messages to rabbitmq exchanges.
According to rabbitmq documentation:
https://www.rabbitmq.com/tutorials/tutorial-three-dotnet
using System.Text;
using RabbitMQ.Client;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
channel.ExchangeDeclare(exchange: "logs", type: ExchangeType.Fanout);
var message = GetMessage(args);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "logs",
routingKey: string.Empty,
basicProperties: null,
body: body);
Console.WriteLine($" [x] Sent {message}");
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
Which works but may concern is about managing open connection.
Whenever I hit method with publish it has to open connection before.
Shouldn’t I open rabbitmq connection once (during app launch for exmaple) a inject it into my class?
Then using only channel in my class?
using var channel = connection.CreateModel();