Easy RabbitMQ Messaging with EasyNetQ
This article is about EasyNetQ, a library for using RabbitMQ with .NET. At the time of writing this article, EasyNetQ is still prerelease software. Despite that, several people have been using it in production environments for a while. If you use it in production, it’s at your own risk.
Last August I wrote “Getting Started with RabbitMQ with .NET“. This illustrated, among other things, how you can receive messages from a RabbitMQ queue using the official RabbitMQ .NET client library.
While that’s a perfectly fine thing to do, it’s often preferable to use a higher-level library that takes care of the little details and allows you to focus on the actual messaging in a structured way. Well, that’s why EasyNetQ exists. With EasyNetQ, you don’t have to worry about stuff like serialization, messaging patterns, connection reliability, etc. In the rest of this article, we’ll see how easy it is to send and receive messages with EasyNetQ.
The first thing we need to do, of course, is to install the EasyNetQ NuGet package:
Then, let’s create a class that represents the messages we will be sending. In this example, we’ll pretend we’re sending the position of a player in a game:
public class PlayerPosition
{
public int X { get; set; }
public int Y { get; set; }
}
Once we have that, we can subscribe to messages arriving in a queue. This is done like this:
using (var bus = RabbitHutch.CreateBus("host=localhost"))
{
bus.Subscribe<PlayerPosition>("MyGame", playerPosition =>
Console.WriteLine($"{playerPosition.X}, {playerPosition.Y}"));
Console.ReadLine();
}
So you declare your queue using RabbitHutch.CreateBus()
. Bus is just a queue. So what’s a RabbitHutch
? Well, what can I say? It’s a rabbit cage.
Note how we are subscribing specifically to receive a message of type PlayerPosition
. We are using a subscriptionId of “MyGame”, and we are also passing in a handler that specifies what to do with the received message. If the syntax looks a little unfamiliar, that’s because I’m using string interpolation, a new C# language feature introduced in C# 6.0.
When we run this, we can see that a queue is created, with the name built from the message class name and the subscriptionId:
Now in order to understand what’s going through the queue, let’s stop the subscriber for the time being.
We can send a message on the queue as follows:
using (var bus = RabbitHutch.CreateBus("host=localhost"))
{
var playerPosition = new PlayerPosition() { X = 1, Y = 2 };
bus.Publish<PlayerPosition>(playerPosition);
Console.ReadLine();
}
Note how we’re sending a strongly typed object (as opposed to a loose string), which we supply as the generic parameter. You can of course leave out the generic parameter, but I am including it above for clarity.
We can now check out the message in the queue from the “Get messages” section in the RabbitMQ management web interface:
As you can see, the data we sent is serialized into JSON, and it is also accompanied by type information that helps EasyNetQ to deserialize it at the receiving end.
In fact, we can now run our subscriber again, and see that it consumes the message:
So you can see how EasyNetQ makes it really easy to focus on the messaging and leave the details to it. Naturally, there’s a lot more you can do with EasyNetQ than this introductory article illustrates. Check out the EasyNetQ documentation for more info.
Reference: | Easy RabbitMQ Messaging with EasyNetQ from our NCG partner Daniel DAgostino at the Gigi Labs blog. |