title | description | ms.date | ms.topic |
---|---|---|---|
Deploy a .NET Aspire project that connects to Redis Cache to Azure |
Learn how to deploy a .NET Aspire project that connects to Redis Cache to Azure |
08/12/2024 |
how-to |
In this tutorial, you learn to configure a .NET Aspire project with a Redis Cache for deployment to Azure. .NET Aspire provides multiple caching integration configurations that provision different Redis services in Azure. You'll learn how to:
[!div class="checklist"]
- Configure the app to provision an Azure Cache for Redis
- Configure the app to provision a containerized Redis Cache
Note
This document focuses specifically on .NET Aspire configurations to provision and deploy Redis Cache resources in Azure. For more information and to learn more about the full .NET Aspire deployment process, see the Azure Container Apps deployment tutorial.
[!INCLUDE aspire-prereqs]
Follow the Tutorial: Implement caching with .NET Aspire integrations to create the sample project.
.NET Aspire provides two built-in configuration options to streamline Redis Cache deployment on Azure:
- Provision a containerized Redis Cache using Azure Container Apps
- Provision an Azure Cache for Redis instance
Add the appropriate .NET Aspire integration to the AspireRedis.AppHost project for your desired hosting service.
Add the 📦 Aspire.Hosting.Azure.Redis NuGet package to the AspireRedis.AppHost project:
dotnet add package Aspire.Hosting.Azure.Redis
Add the 📦 Aspire.Hosting.Redis NuGet package to the AspireRedis.AppHost project:
dotnet add package Aspire.Hosting.Redis
Configure the AspireRedis.AppHost project for your desired Redis service.
Replace the contents of the :::no-loc text="Program.cs"::: file in the AspireRedis.AppHost project with the following code:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddAzureRedis("cache");
var apiService = builder.AddProject<Projects.AspireRedis_ApiService>("apiservice")
.WithReference(cache);
builder.AddProject<Projects.AspireRedis_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(cache)
.WaitFor(cache)
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
The preceding code adds an Azure Cache for Redis resource to your app and configures a connection called cache
. The AddAzureRedis
method ensures that tools such as the Azure Developer CLI or Visual Studio create an Azure Cache for Redis resource during the deployment process.
Replace the contents of the :::no-loc text="Program.cs"::: file in the AspireRedis.AppHost project with the following code:
var builder = DistributedApplication.CreateBuilder(args);
var cache = builder.AddRedis("cache");
var apiService = builder.AddProject<Projects.RedisSample_ApiService>("apiservice")
.WithReference(cache);
builder.AddProject<Projects.RedisSample_Web>("webfrontend")
.WithExternalHttpEndpoints()
.WithReference(cache)
.WaitFor(cache)
.WithReference(apiService)
.WaitFor(apiService);
builder.Build().Run();
The preceding code adds a Redis Container resource to your app and configures a connection called cache
. This configuration also ensures that tools such as the Azure Developer CLI or Visual Studio create a containerized Redis instance during the deployment process.
Tools such as the Azure Developer CLI (azd
) support .NET Aspire Redis integration configurations to streamline deployments. azd
consumes these settings and provisions properly configured resources for you.
Note
You can also use the Azure CLI or Bicep to provision and deploy .NET Aspire project resources. These options require more manual steps, but provide more granular control over your deployments. .NET Aspire projects can also connect to an existing Redis instance through manual configurations.
-
Open a terminal window in the root of your .NET Aspire project.
-
Run the
azd init
command to initialize the project withazd
.azd init
-
When prompted for an environment name, enter docs-aspireredis.
-
Run the
azd up
command to begin the deployment process:azd up
-
Select the Azure subscription that should host your app resources.
-
Select the Azure location to use.
The Azure Developer CLI provisions and deploys your app resources. The process may take a few minutes to complete.
-
When the deployment finishes, click the resource group link in the output to view the created resources in the Azure portal.
The deployment process provisioned an Azure Cache for Redis resource due to the .AppHost configuration you provided.
:::image type="content" loc-scope="azure" source="media/resources-azure-redis.png" alt-text="A screenshot showing the deployed Azure Cache for Redis.":::
The deployment process created a Redis app container due to the .AppHost configuration you provided.
:::image type="content" loc-scope="azure" source="media/resources-azure-redis-container.png" alt-text="A screenshot showing the containerized Redis.":::
[!INCLUDE clean-up-resources]