Loading...

Installing and running Stackexchange.Redis client library on Linux environments

Installing and running Stackexchange.Redis client library on Linux environments

 

Overview

StackExchange.Redis is a high performance general purpose redis client for .NET languages (C#, etc.), and is designed for Windows / .NET SDK environments.
On other hand, it is possible now install .NET SDK and run PowerShell commands on Linux environments.
This article describes how to install and link all together to have a Linux environment to run some commands and connect to any Redis service using StackExchange.Redis client library.
This can be used for test proposes, and there are no support to run this on production.
Try and test this at your own risk and responsibility.
This was tested on Linux Ubuntu v20.04.02 LTS, .NET SDK v7.0.403, Stackexchange.Redis v2.7.4, and PowerShell v7.3.9.0.
There are no guarantees this will work with all scenarios and versions.

 

StackExchange.Redis features

  • High performance multiplexed design, allowing for efficient use of shared connections from multiple calling threads
  • Abstraction over redis node configuration: the client can silently negotiate multiple redis servers for robustness and availability
  • Convenient access to the full redis feature-set
  • Full dual programming model both synchronous and asynchronous usage, without requiring “sync over async” usage of the TPL
  • Support for redis “cluster”


StackExchange.Redis implementation on Linux Ubuntu

To have a working scenario on a Linux VM , be able to connect to some Redis server using StackExchange.Redis client library, follow these steps:

 

  1. Import the Microsoft repository 

 

wget -q https://packages.microsoft.com/config/ubuntu/20.04/packages-microsoft-prod.deb sudo dpkg -i packages-microsoft-prod.deb

 

 

  1. If needed, Installing PowerShell on Ubuntu - PowerShell | Microsoft Learn
  2. Install the .NET SDK

Open a terminal and run the following commands to install the .NET Core SDK on your Ubuntu 20.04 system.

You can also choose a different version of the .NET SDK, but version 3.1, is compatible with StackExchange.Redis.

 

sudo apt update sudo apt install -y dotnet-sdk-3.1

 

 

  1. Create a project directory:

Create a directory for your project, navigate to it, and create a PowerShell script file with a .ps1 extension.

 

mkdir my-redis-project cd my-redis-project touch connect-redis.ps1

 

 

  1. Install the StackExchange.Redis library:

This will create a basic .NET Core console application and add the StackExchange.Redis package to your project.

 

sudo dotnet new console sudo dotnet add package StackExchange.Redis

 

 

  1. Edit your PowerShell script:

Open the connect-redis.ps1 script in a text editor (ex: vi) and add your PowerShell code to connect to the Redis server, using the StackExchange.Redis library.

 

# Load the StackExchange.Redis library Add-Type -Path "/home/<user>/my-redis-project/bin/Debug/net7.0/StackExchange.Redis.dll" # see note (**) below to find StackExchange.Redis.dll location # Define the Redis server endpoint and SSL port used (example for Azure Cahe for Redis service) $redisServerEndpoint = "<redisName>.redis.cache.windows.net:6380" # Create a ConfigurationOptions instance to configure the connection # https://stackexchange.github.io/StackExchange.Redis/Configuration.html#configuration-options $redisConfig = New-Object StackExchange.Redis.ConfigurationOptions $redisConfig.EndPoints.Add($redisServerEndpoint) $redisConfig.Password = "<redisAccessKey>" # Replace with your actual Redis password # Create a connection to the Redis server $redisConnection = [StackExchange.Redis.ConnectionMultiplexer]::Connect($redisConfig) # Get a reference to the Redis database (by default, database 0) $redisDatabase = $redisConnection.GetDatabase() # Perform Redis operations $redisDatabase.StringSet("mykey", "Hello, Redis from Linux PowerShell!") $value = $redisDatabase.StringGet("mykey") Write-Host "Redis Value: $value" # Close the connection $redisConnection.Close()

 

 

6. Run your PowerShell script:

 

pwsh connect-redis.ps1

 

 

 

Useful commands:  

To check Linux distribution and version:

 

lsb_release -a

 

 

To check PowerShell installed version:

 

pwsh -version

 

 

To check installed .NET SDKs on your system:

 

dotnet --list-sdks

 

 

To check Stackexchange.Redis installed version:

 

sudo dotnet new console sudo dotnet list package

 


To check default .NET SDK version

 

sudo dotnet --version

 


To rebuild your project, if needed, and to check the path for StackExchange.Redis.dll provided in the PS script (**)

 

sudo dotnet build

 

(**) You should obtain something like this:
my-redis-project -> /home/<user>/my-redis-project/bin/Debug/net7.0/my-redis-project.dll
StackExchange.Redis.dll should be in the same path


Install the required .NET dependencies, if needed:

 

sudo dotnet restore

 


Check the .ps1 file content for any typo:

 

cat ./connect-redis.ps1

 


Update StackExchange.Redis, if needed:

 

sudo dotnet add package StackExchange.Redis --version <new_version>

 

 

 

Conclusion:  

Despite the target of this article describes not being to deploy solutions for production environments, this is a good way to test connectivity and run some Redis commands on a Redis service, from Linux environments, using Stackexchange.Redis client library.

I hope this can be useful!!!

 

Related documentation:  
StackExchange.Redis versions
StackExchange.Redis release notes
Installing PowerShell on Ubuntu - PowerShell | Microsoft Learn


 

 

Published on:

Learn more
Need help with this product?

We can help you with Installing and running Stackexchange.Redis client library on Linux environments

If you want help implementing, troubleshooting, or improving this product, contact us and we’ll point you in the right direction.

Azure PaaS Blog articles
Azure PaaS Blog articles

Azure PaaS Blog articles

Share post:

Related posts

Azure Storage - TLS 1.0 and 1.1 retirement

Overview TLS 1.0 and 1.1 retirement on Azure Storage was previously announced for Nov 1st, 2024, and it was postponed recently to 1 year later...

1 year ago

Efficient Management of Append and Page Blobs Using Azure Storage Actions

  Overview In Azure Storage, Blob Lifecycle Management (BLM) allows you to automate the management of your data based on rules defined by...

1 year ago

[Azure AI Search] Internal Server Error when creating CMK encrypted objects

Scenario Customers follow the Microsoft doc to create CMK encrypted objects (data source, index etc.), but get the 500 Internal Serv...

1 year ago

Optimizing Azure Table Storage: Automated Data Cleanup using a PowerShell script with Azure Automate

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Optimizing Azure Table Storage: Automated Data Clean-up using a PowerShell script with Azure Automat

Scenario This blog’s aim is to manage Table Storage data efficiently. Imagine you have a large Azure Table Storage that accumulates logs from ...

1 year ago

Restoring Soft-Deleted Blobs with multithreading in Azure Storage Using C#

Blob soft delete is an essential feature that safeguards your data against accidental deletions or overwrites. By retaining deleted data for a...

1 year ago

Performing simple Azure Table Storage REST API operations using curl command.

The blog provides guidance to perform simple Table Storage REST API operations such as Create table, Delete Table, Insert entity, Delete entit...

1 year ago

Bulk delete all the old jobs from the batch account

Deleting a Job also deletes all Tasks that are part of that Job, and all Job statistics. This also overrides the retention period for Task dat...

1 year ago

Utilizing Azure Storage and Runbooks for scheduled automated backups of Azure SQL Databases

In this article, we are going to provide detailed steps to create a scheduled Azure SQL Database backup to storage account using automation. T...

2 years ago

[Azure Service Bus] JMS messages getting dead-lettered

The article discusses a problem where numerous messages end up in the dead letter queue (DLQ) when the JMS service bus consumer connects to th...

2 years ago

Newsletter

Get the latest Dynamics 365 and Power Platform content in your inbox

A curated digest of community blogs, product news, videos, and podcasts — delivered without the noise.

Weekly updates Unsubscribe anytime Fresh community picks
We use your email only for the newsletter and you can unsubscribe at any time.
By subscribing, you agree to the privacy policy.