-
Notifications
You must be signed in to change notification settings - Fork 152
adding deployment steps to .NET Aspire + Redis to Azure #883
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
--- | ||
title: Deploy a .NET Aspire app that connects to Redis Cache to Azure | ||
description: Learn how to deploy a .NET Aspire app that connects to Redis Cache to Azure | ||
ms.date: 05/12/2024 | ||
ms.topic: how-to | ||
--- | ||
|
||
# Tutorial: Deploy a .NET Aspire app with a Redis Cache to Azure | ||
|
||
In this tutorial, you learn to configure a .NET Aspire app with a Redis Cache for deployment to Azure. .NET Aspire provides multiple caching component 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. Visit the [Azure Container Apps deployment](/dotnet/aspire/deployment/azure/aca-deployment?branch=pr-en-us-532&tabs=visual-studio%2Clinux%2Cpowershell&pivots=azure-azd) tutorial to learn more about the full .NET Aspire deployment process. | ||
|
||
[!INCLUDE [aspire-prereqs](../includes/aspire-prereqs.md)] | ||
|
||
## Create the sample solution | ||
|
||
Follow the [Tutorial: Implement caching with .NET Aspire components](./caching-components.md) 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 component to the app | ||
|
||
Add the appropriate .NET Aspire component to the _AspireRedis.AppHost_ project for your desired hosting service. | ||
|
||
# [Azure Cache for Redis](#tab/azure-redis) | ||
|
||
Add the [Aspire.Hosting.Azure.Redis](https://www.nuget.org/packages/Aspire.Hosting.Azure.Redis) package to the _AspireRedis.AppHost_ project: | ||
|
||
```dotnetcli | ||
dotnet add package Aspire.Hosting.Azure.Redis --prerelease | ||
``` | ||
|
||
## [Redis Container](#tab/redis-container) | ||
|
||
Add the [Aspire.Hosting.Redis](https://www.nuget.org/packages/Aspire.Hosting.Redis) package to the _AspireRedis.AppHost_ project: | ||
|
||
```dotnetcli | ||
dotnet add package Aspire.Hosting.Redis --prerelease | ||
``` | ||
|
||
--- | ||
|
||
### Configure the AppHost project | ||
|
||
Configure the _AspireRedis.AppHost_ project for your desired Redis service. | ||
|
||
# [Azure Cache for Redis](#tab/azure-redis) | ||
|
||
Replace the contents of the _Program.cs_ file in the _AspireRedis.AppHost_ project with the following code: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var cache = builder.AddRedis("cache").PublishAsAzureRedis(); | ||
CawaMS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
var apiService = builder.AddProject<Projects.AspireRedis_ApiService>("apiservice") | ||
.WithReference(cache); | ||
|
||
builder.AddProject<Projects.AspireRedis_Web>("webfrontend") | ||
.WithExternalHttpEndpoints() | ||
.WithReference(cache) | ||
.WithReference(apiService); | ||
|
||
builder.Build().Run(); | ||
``` | ||
|
||
The preceding code adds an Azure Cache for Redis resource to your app and configures a connection called `cache`. The `PublishAsAzureRedis` method ensures that tools such as the Azure Developer CLI or Visual Studio create an Azure Cache for Redis resource during the deployment process. | ||
|
||
## [Redis Container](#tab/redis-container) | ||
|
||
Replace the contents of the _Program.cs_ file in the _AspireRedis.AppHost_ project with the following code: | ||
|
||
```csharp | ||
var builder = DistributedApplication.CreateBuilder(args); | ||
|
||
var cache = builder.AddRedis("cache"); | ||
|
||
var apiService = builder.AddProject<Projects.RedisSample_ApiService>("apiservice") | ||
.WithReference(cache); | ||
CawaMS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
builder.AddProject<Projects.RedisSample_Web>("webfrontend") | ||
.WithExternalHttpEndpoints() | ||
.WithReference(cache) | ||
.WithReference(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](/azure/developer/azure-developer-cli/overview) (`azd`) support .NET Aspire Redis component configurations to streamline deployments. `azd` consumes these settings and provisions properly configured resources for you. | ||
|
||
> [!NOTE] | ||
> You can also use the [Azure CLI](/dotnet/aspire/deployment/azure/aca-deployment?branch=pr-en-us-532&tabs=visual-studio%2Clinux%2Cpowershell&pivots=azure-cli) or [Bicep](/dotnet/aspire/deployment/azure/aca-deployment?branch=pr-en-us-532&tabs=visual-studio%2Clinux%2Cpowershell&pivots=azure-bicep) to provision and deploy .NET Aspire app resources. These options require more manual steps, but provide more granular control over your deployments. .NET Aspire apps can also connect to an existing Redis instance through manual configurations. | ||
CawaMS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
1. Open a terminal window in the root of your .NET Aspire project. | ||
|
||
1. Run the `azd init` command to initialize the project with `azd`. | ||
|
||
```azdeveloper | ||
azd init | ||
``` | ||
|
||
1. When prompted for an environment name, enter *docs-aspireredis*. | ||
|
||
1. Run the `azd up` command to begin the deployment process: | ||
|
||
```azdeveloper | ||
azd up | ||
``` | ||
|
||
1. Select the Azure subscription that should host your app resources. | ||
|
||
1. 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. | ||
|
||
1. When the deployment finishes, click the resource group link in the output to view the created resources in the Azure portal. | ||
|
||
## [Azure Cache for Redis](#tab/azure-redis) | ||
|
||
The deployment process provisioned an Azure Cache for Redis resource due to the **.AppHost** configuration you provided. | ||
|
||
:::image type="content" source="media/resources-azure-redis.png" alt-text="A screenshot showing the deployed Azure Cache for Redis."::: | ||
|
||
## [Redis Container](#tab/redis-container) | ||
|
||
The deployment process created a Redis app container due to the **.AppHost** configuration you provided. | ||
|
||
:::image type="content" source="media/resources-azure-redis-container.png" alt-text="A screenshot showing the containerized Redis."::: | ||
|
||
--- | ||
|
||
[!INCLUDE [clean-up-resources](../includes/clean-up-resources.md)] | ||
|
||
## See also | ||
|
||
- [.NET Aspire deployment via Azure Container Apps](../deployment/azure/aca-deployment.md) | ||
- [.NET Aspire Azure Container Apps deployment deep dive](../deployment/azure/aca-deployment-azd-in-depth.md) | ||
- [Deploy a .NET Aspire app using GitHub Actions](../deployment/azure/aca-deployment-github-actions.md) |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.