Azure Cache Redis in Aspire on Local PC

In this exercise, I focused on only one Azure resource: Azure Managed Redis. My goal was simple: run Redis locally as a container,
connect a console client project to it through .NET Aspire with access key authentication, and validate that the client can set and retrieve cache values end-to-end.

How it looks like

This is the Redis container view

How I wired Azure Managed Redis in Aspire AppHost

In AzureEmulators/AppHost.cs, I added Azure Managed Redis as a container with access key authentication enabled.
Then I connected the Redis client project with WithReference(azureManagedRedis) and WaitFor(azureManagedRedis).

  • Azure Managed Redis container resource: azureManagedRedis1
  • Authentication: WithAccessKeyAuthentication()
  • Client project reference: azureManagedRedisClient1
var azureManagedRedis = builder.AddAzureManagedRedis("azureManagedRedis1")
      .RunAsContainer()
       .WithAccessKeyAuthentication()
    ;

builder.AddProject<Projects.AzureManagedRedisClient>("azureManagedRedisClient1")
    .WithReference(azureManagedRedis)
    .WaitFor(azureManagedRedis)
    ;

What my AzureManagedRedisClient project does

In AzureManagedClient/Program.cs, I implemented a startup flow to validate Redis connectivity and demonstrate caching operations.

  • I scan environment variables for the Azure Managed Redis connection string.
  • I fail fast if the connection string is missing.
  • I create a ConnectionMultiplexer using that connection string (StackExchange.Redis library).
  • I get a database reference from the multiplexer.
  • I call StringSetAsync("myName", "Andrei") to store a cache value.
  • I call StringGetAsync("myName") to retrieve the cache value and verify it was stored.

Code from my console client project

using StackExchange.Redis;
using System.Collections;

Console.WriteLine("Hello, Azure Managed Redis Client!");

string connectionStringAzureManagedRedis = "";
foreach (var item in Environment.GetEnvironmentVariables().Cast<DictionaryEntry>())
{
    if (item.Key?.ToString()?.Contains("azureManagedRedis") == true)
    {
        Console.WriteLine($"{item.Key}: {item.Value}");
        connectionStringAzureManagedRedis = item.Value?.ToString() ?? string.Empty;
    }
}
if (string.IsNullOrWhiteSpace(connectionStringAzureManagedRedis))
{
    Console.WriteLine("Azure Managed Redis connection string is not set in environment variables.");
    return;
}
Console.WriteLine($"Connection string for Azure Managed Redis: {connectionStringAzureManagedRedis}");
var mux = await ConnectionMultiplexer.ConnectAsync(connectionStringAzureManagedRedis);

var db = mux.GetDatabase();
Console.WriteLine("Connected to Azure Managed Redis. Setting and getting a value...");
await db.StringSetAsync("myName", "Andrei");
Console.WriteLine("Set value for 'myName' to 'Andrei' in Azure Managed Redis.");
var value = await db.StringGetAsync("myName");
Console.WriteLine($"Retrieved value for 'myName' from Azure Managed Redis: {value}");

Output logs from my client project

Waiting for resource 'azureManagedRedis1' to enter the 'Running' state.
Waiting for resource 'azureManagedRedis1' to become healthy.
Waiting for resource ready to execute for 'azureManagedRedis1'.
Finished waiting for resource 'azureManagedRedis1'.
Hello, Azure Managed Redis Client!
ConnectionStrings__azureManagedRedis1: localhost:54593,password=UvpWjPp6hZpYCgNSEHsZ0K,ssl=true
Connection string for Azure Managed Redis: localhost:54593,password=UvpWjPp6hZpYCgNSEHsZ0K,ssl=true
Connected to Azure Managed Redis. Setting and getting a value...
Set value for 'myName' to 'Andrei' in Azure Managed Redis.
Retrieved value for 'myName' from Azure Managed Redis: Andrei

What is achieved

  • I can run Azure Managed Redis on my local PC as a container without provisioning a cloud Redis cache instance.
  • I get dependency wiring from Aspire between AppHost and AzureManagedRedisClient.
  • I proved set and get operations by storing and retrieving a cache value from the client project using StackExchange.Redis.
  • I now have a repeatable local loop for testing caching patterns and performance before deploying to Azure.

More links

Azure Cache for Redis – Get Started


Posted

in

, ,

by

Tags: