Edit

Share via


Tutorial: Deploy an Aspire project with a Redis Cache to Azure

In this tutorial, you learn to configure an Aspire project with a Redis Cache for deployment to Azure. Aspire provides multiple caching integration configurations that provision different Redis services in Azure. You'll learn how to:

  • 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 Aspire configurations to provision and deploy Redis Cache resources in Azure. For more information and to learn more about the full Aspire deployment process, see the Azure Container Apps deployment tutorial.

Prerequisites

To work with Aspire, you need the following installed locally:

For more information, see Aspire setup and tooling, and Aspire SDK.

Create the sample solution

Follow the Tutorial: Implement caching with Aspire integrations to create the sample project.

Configure the app for Redis cache deployment

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 Aspire integration to the app

Add the appropriate 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

Configure the AppHost project

Configure the AspireRedis.AppHost project for your desired Redis service.

Replace the contents of the 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.

Deploy the app

The aspire deploy command supports Aspire Redis integration configurations to streamline deployments. The command consumes these settings and provisions properly configured resources for you.

Note

You can also use the Azure CLI or Bicep to provision and deploy Aspire project resources. These options require more manual steps, but provide more granular control over your deployments. Aspire projects can also connect to an existing Redis instance through manual configurations.

To deploy your app to Azure Container Apps, run the following command from the AspireRedis.AppHost directory:

aspire deploy

When you run the aspire deploy command for the first time, you'll be prompted to:

  1. Sign in to Azure: Follow the authentication prompts to sign in to your Azure account.
  2. Select a subscription: Choose the Azure subscription you want to use for deployment.
  3. Select or create a resource group: Choose an existing resource group or create a new one.
  4. Select a location: Choose the Azure region where you want to deploy your resources.

The deployment process will provision the necessary Azure resources and deploy your Aspire app. The process may take a few minutes to complete.

When the deployment finishes, the command output will provide information about the deployed resources that you can view in the Azure portal.

The deployment process provisioned an Azure Cache for Redis resource due to the .AppHost configuration you provided.

A screenshot showing the deployed Azure Cache for Redis.

Clean up resources

Run the following Azure CLI command to delete the resource group when you no longer need the Azure resources you created. Deleting the resource group also deletes the resources contained inside of it.

az group delete --name <your-resource-group-name>

For more information, see Clean up resources in Azure.

See also