Skip to content

Latest commit

 

History

History
160 lines (99 loc) · 6.42 KB

caching-integrations-deployment.md

File metadata and controls

160 lines (99 loc) · 6.42 KB
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

Tutorial: Deploy a .NET Aspire project with a Redis Cache to Azure

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]

Create the sample solution

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

Configure the app for Redis cache deployment

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

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 AppHost project

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.


Deploy the app

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.

  1. Open a terminal window in the root of your .NET Aspire project.

  2. Run the azd init command to initialize the project with azd.

    azd init
    
  3. When prompted for an environment name, enter docs-aspireredis.

  4. Run the azd up command to begin the deployment process:

    azd up
    
  5. Select the Azure subscription that should host your app resources.

  6. 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.

  7. 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]

See also