Getting the Version of Redis Connected in C# & .NET
[C#, .NET, Redis, StackExchange.Redis]
Now that we know the process of “Getting the Version of RabbitMQ Connected In C# & .NET”, I wondered if it was possible to do the same for Redis.
For debugging and troubleshooting purposes, it may be necessary to retrieve this information at runtime from within your application.
And the answer is indeed, this is very possible.
You send the server the INFO command, and read the returned response.
We do this using the built-in functionality of the StackExchange.Redis package.
The response is a structure that is a nested grouping of KeyValue pairs.
You can extract the version as follows:
// Adjust your connection string as needed
using StackExchange.Redis;
// Get a connection to the Redis server
await using (var connection =
await ConnectionMultiplexer.ConnectAsync("localhost:6379,password=YourStrongPassword123,allowAdmin=true"))
{
// Retrieve the server
var server = connection.GetServer("localhost", 6379);
// Execute the INFO command
var version = server.Info("Server")
.SelectMany(g => g)
.FirstOrDefault(p => p.Key == "redis_version").Value;
Console.WriteLine($"Version: {version}");
}
This will print something like this:
Redis Version: 8.2.1
The caveat of this technique is that you must connect to Redis with admin mode enabled by passing the value allowAdmin=true
to the connection string.
This, naturally, is probably not going to be possible in production for obvious reasons - security!
We will see how to achieve that in the next post.
TLDR
The StackExchange.Redis
package offers a Server
object against which you can call the Info
method to retrieve Server configuration information.
The code is in my GitHub.
Happy hacking!