The RabbitMQ .NET client is an implementation of an AMQP client library for C# (and, implicitly, other .NET languages).
The RabbitMQ .NET client is maintained by RabbitMQ and can be included in your project as a package using the NuGet package manager.
First of all we need to connect to the server. Add the namespace RabbitMQ.Client to your file and create a ConnectionFactory Set the URI to the CloudAMQP url. The URL can be found in the control panel details for your instance.
..
using RabbitMQ.Client;
// CloudAMQP URL in format amqp://user:pass@hostName:port/vhost
private static readonly string _url = "amqp://guest:guest@localhost/%2f";
// Create a ConnectionFactory and set the Uri to the CloudAMQP url
// the connectionfactory is stateless and can safetly be a static resource in your app
var factory = new ConnectionFactory
{
Uri = new Uri(url)
};
// create a connection and open a channel, dispose them when done
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
// ensure that the queue exists before we publish to it
var queueName = "queue1";
bool durable = false;
bool exclusive = false;
bool autoDelete = true;
channel.QueueDeclare(queueName, durable, exclusive, autoDelete, null);
// read message from input
var message = Console.ReadLine();
// the data put on the queue must be a byte array
var data = Encoding.UTF8.GetBytes(message);
// publish to the "default exchange", with the queue name as the routing key
var exchangeName = "";
var routingKey = queueName;
channel.BasicPublish(exchangeName, routingKey, null, data);
..
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
public class Consumer
{
private IConnection _connection;
private IModel _channel;
private ManualResetEvent _resetEvent = new ManualResetEvent(false);
public void ConsumeQueue()
{
// CloudAMQP URL in format amqp://user:pass@hostName:port/vhost
string _url = "amqp://guest:guest@localhost/%2f";
// create a connection and open a channel, dispose them when done
var factory = new ConnectionFactory
{
Uri = new Uri(url)
};
_connection = factory.CreateConnection();
_channel = _connection.CreateModel();
// ensure that the queue exists before we access it
var queueName = "queue1";
bool durable = false;
bool exclusive = false;
bool autoDelete = true;
_channel.QueueDeclare(queueName, durable, exclusive, autoDelete, null);
var consumer = new EventingBasicConsumer(_channel);
// add the message receive event
consumer.Received += (model, deliveryEventArgs) =>
{
var body = deliveryEventArgs.Body.ToArray();
// convert the message back from byte[] to a string
var message = Encoding.UTF8.GetString(body);
Console.WriteLine("** Received message: {0} by Consumer thread **", message);
// ack the message, ie. confirm that we have processed it
// otherwise it will be requeued a bit later
_channel.BasicAck(deliveryEventArgs.DeliveryTag, false);
};
// start consuming
_ = _channel.BasicConsume(consumer, queueName);
// Wait for the reset event and clean up when it triggers
_resetEvent.WaitOne();
_channel?.Close();
_channel = null;
_connection?.Close();
_connection = null;
}
}
The full .NET C# code can be seen at https://github.com/cloudamqp/dotnetcore-amqp-example.