Getting the Version of RabbitMQ Connected in C# & .NET
[C#, .NET, RabbitMQ, EasyNetQ]
Recently, I was troubleshooting some code using RabbitMQ & EasyNetQ and found myself wondering how to verify the version of RabbitMQ I was connected to.
The simplest way is as follows:
- Create a connection to the RabbitMQ instance
- Fetch the server properties
- Read the version property
This is stored as a byte
array, that you will need to decode.
The code is as follows:
using System.Text;
using RabbitMQ.Client;
// Set the username, password & port
const string username = "test";
const string password = "test";
const short port = 5672;
// Create factory to use for connection
var factory = new ConnectionFactory
{
Uri = new Uri($"amqp://{username}:{password}@localhost:{port}/")
};
// Get a connection
using var connection = factory.CreateConnection();
// Fetch the server properties
var serverProperties = connection.ServerProperties;
// Fetch the version
if (serverProperties.TryGetValue("version", out var versionBytes))
{
// Decode the version
var version = Encoding.UTF8.GetString((byte[])versionBytes);
// Print version info
Console.WriteLine($"RabbitMQ Version: {version}");
}
else
{
Console.WriteLine("Error fetching version");
}
This should print something like this:
RabbitMQ Version: 4.1.4
If you are not using EasyNetQ
, you can directly reference the RabbitMQ.Client package.
TLDR
You can access the version of RabbitMQ from the ServerProperties
object of the IConnection
.
The code is in my GitHub.
Happy hacking!