LavinMQ with .NET

The RabbitMQ .NET client can be used with LavinMQ. It is an implementation of an AMQP client library for C# (and, implicitly, other .NET languages).

The .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 your host url.

Code example

Publish

..
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);

Subscribe

..
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.

Ready to take the next steps? Here are some things you should keep in mind:

Managed LavinMQ instance on CloudAMQP

LavinMQ has been built with performance and ease of use in mind - we've benchmarked a throughput of about 1,000,000 messages/sec. You can try LavinMQ without any installation hassle by creating a free instance on CloudAMQP. Signing up is a breeze.

Help and feedback

We welcome your feedback and are eager to address any questions you may have about this piece or using LavinMQ. Join our Slack channel to connect with us directly. You can also find LavinMQ on GitHub.