title | description | ms.devlang | ms.custom | ms.topic | ms.date | zone_pivot_groups | appliesto | ||
---|---|---|---|---|---|---|---|---|---|
Quickstart: Use Azure Cache for Redis in .NET Core | In this quickstart, learn how to access Azure Cache for Redis in your .NET Core apps | csharp | devx-track-csharp, mvc, mode-other, devx-track-dotnet, ignite-2024 | quickstart | 12/20/2024 | redis-type |
|
In this quickstart, you incorporate Azure Cache for Redis into a .NET Core app to have access to a secure, dedicated cache that is accessible from any application within Azure. You specifically use the StackExchange.Redis client with C# code in a .NET Core console app.
Clone the repo https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core on GitHub.
- Azure subscription - create one for free
- .NET Core SDK
::: zone pivot="azure-managed-redis"
[!INCLUDE managed-redis-create]
::: zone-end
::: zone pivot="azure-cache-redis"
[!INCLUDE redis-cache-create]
::: zone-end
[!INCLUDE cache-entra-access]
The Azure.StackExchange.Redis library contains the Microsoft Entra ID authentication method for connecting to Azure Redis services using Microsoft Entra ID. It's applicable to all Azure Cache for Redis, Azure Cache for Redis Enterprise, and Azure Managed Redis (Preview).
dotnet add package Microsoft.Azure.StackExchangeRedis
Include the libraries in your code
usingAzure.Identity;usingStackExchange.Redis
Using the default Azure credentials to authenticate the client connection. This enables your code to use the signed-in user credential when running locally, and an Azure managed identity when running in Azure without code change.
varconfigurationOptions=awaitConfigurationOptions.Parse($"{_redisHostName}").ConfigureForAzureWithTokenCredentialAsync(newDefaultAzureCredential());ConnectionMultiplexer_newConnection=awaitConnectionMultiplexer.ConnectAsync(configurationOptions);IDatabaseDatabase=_newConnection.GetDatabase();
::: zone pivot="azure-managed-redis"
Edit the Web.config file. Then add the following content:
"_redisHostName":"<cache-hostname>"
Replace
<cache-hostname>
with your cache host name as it appears in the Overview section of the Resource menu in the Azure portal.For example, with Azure Managed Redis or the Enterprise tiers: my-redis.eastus.azure.net:10000
Save the file.
For more information, see StackExchange.Redis and the code in a GitHub repo.
::: zone-end
::: zone pivot="azure-cache-redis"
Edit the appsettings.json file. Then add the following content:
"_redisHostName":"<cache-hostname>"
Replace
<cache-hostname>
with your cache host name as it appears in the Overview section of the Resource menu in the Azure portal.For example, with Azure Cache for Redis: my-redis.eastus.azure.net:6380
Save the file.
For more information, see StackExchange.Redis and the code in a GitHub repo. ::: zone-end
If you opened any files, save them, and build the app with the following command:
dotnet build
To test serialization of .NET objects, run the app with the following command:
dotnet run
:::image type="content" source="media/dotnet-core-quickstart/cache-console-app-complete.png" alt-text="Screenshot sowing console app completed.":::
[!INCLUDE cache-delete-resource-group]