Skip to content

Latest commit

 

History

History
134 lines (83 loc) · 4.79 KB

dotnet-core-quickstart.md

File metadata and controls

134 lines (83 loc) · 4.79 KB
titledescriptionms.devlangms.customms.topicms.datezone_pivot_groupsappliesto
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
✅ Azure Managed Redis
✅ Azure Cache for Redis

Quickstart: Use Azure Redis in .NET Core

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.

Skip to the code on GitHub

Clone the repo https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/dotnet-core on GitHub.

Prerequisites

::: zone pivot="azure-managed-redis"

Create an Azure Managed Redis (preview) instance

[!INCLUDE managed-redis-create]

::: zone-end

::: zone pivot="azure-cache-redis"

Create an Azure Cache for Redis instance

[!INCLUDE redis-cache-create]

::: zone-end

[!INCLUDE cache-entra-access]

Install the Library for using Microsoft Entra ID Authentication

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 

Connect to the cache using Microsoft Entra ID

  1. Include the libraries in your code

    usingAzure.Identity;usingStackExchange.Redis
  2. 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"

To edit the appsettings.json file

  1. Edit the Web.config file. Then add the following content:

    "_redisHostName":"<cache-hostname>"
  2. 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

  3. Save the file.

For more information, see StackExchange.Redis and the code in a GitHub repo.

::: zone-end

::: zone pivot="azure-cache-redis"

To edit the appsettings.json file

  1. Edit the appsettings.json file. Then add the following content:

    "_redisHostName":"<cache-hostname>"
  2. 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

  3. Save the file.

For more information, see StackExchange.Redis and the code in a GitHub repo. ::: zone-end

Run the sample

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]

Related content

close