rabbitmq-tutorials/dotnet/NewTask/NewTask.cs
Luke Bakken a4adce94c9
Update dotnet tutorials to use 7.0.0
* Specify unix line endings so that `christian-bromann/docusaurus-theme-github-codeblock` formats code blocks correctly
* Fix RPC examples
2024-11-06 08:44:10 -08:00

27 lines
806 B
C#

using RabbitMQ.Client;
using System.Text;
var factory = new ConnectionFactory { HostName = "localhost" };
using var connection = await factory.CreateConnectionAsync();
using var channel = await connection.CreateChannelAsync();
await channel.QueueDeclareAsync(queue: "task_queue", durable: true, exclusive: false,
autoDelete: false, arguments: null);
var message = GetMessage(args);
var body = Encoding.UTF8.GetBytes(message);
var properties = new BasicProperties
{
Persistent = true
};
await channel.BasicPublishAsync(exchange: string.Empty, routingKey: "task_queue", mandatory: true,
basicProperties: properties, body: body);
Console.WriteLine($" [x] Sent {message}");
static string GetMessage(string[] args)
{
return ((args.Length > 0) ? string.Join(" ", args) : "Hello World!");
}