Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Prepare Azure environments for new workloads—subscriptions, networking, identity, and landing zones
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/services/app-service/templates/recipes/redis/source/dotnet.md
1# Redis Recipe — C# (.NET) — REFERENCE ONLY23## ASP.NET Core Distributed Cache Setup45### NuGet Packages67```xml8<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="8.0.*" />9<PackageReference Include="Azure.Identity" Version="1.13.*" />10```1112### Program.cs (additions)1314Add these lines — do NOT replace the existing file:1516```csharp17using Azure.Identity;18using StackExchange.Redis;1920var redisHost = builder.Configuration["REDIS_HOST"];21var configOptions = ConfigurationOptions.Parse($"{redisHost}:6380");22configOptions.Ssl = true;23configOptions.AbortOnConnectFail = false;2425// ConfigureForAzureWithTokenCredentialAsync handles automatic token renewal26await configOptions.ConfigureForAzureWithTokenCredentialAsync(27new DefaultAzureCredential());2829builder.Services.AddStackExchangeRedisCache(options =>30{31options.ConfigurationOptions = configOptions;32options.InstanceName = "app:";33});34```3536> 💡 `ConfigureForAzureWithTokenCredentialAsync` manages token acquisition and renewal automatically — no manual token refresh required.3738### Usage3940```csharp41using Microsoft.Extensions.Caching.Distributed;4243app.MapGet("/api/cached", async (IDistributedCache cache) =>44{45var value = await cache.GetStringAsync("my-key");46if (value is null)47{48value = "computed-value";49await cache.SetStringAsync("my-key", value,50new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(5) });51}52return Results.Ok(new { value });53});54```5556## Files to Modify5758| File | Action |59|------|--------|60| `Program.cs` | Add Redis cache registration |61| `*.csproj` | Add NuGet packages |62