Ask Learn Preview
Please sign in to use this experience.
Sign inThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this exercise, you'll configure the eShop application to use Azure Cache for Redis and Application Insights when it's deployed to Azure. You'll also deploy the .NET Aspire solution to your Azure subscription.
Important
To complete this exercise, you'll need an Azure subscription. If you don't have a subscription, you can create a free trial subscription.
The prerequisites for .NET Aspire are:
If you've already got these packages installed, you can skip ahead to begin working with Azure Storage.
Follow this .NET 8 link, and select the correct installer for your operating system. For example, if you're using Windows 11, and a modern processor, select the x64 .NET 8 SDK for Windows.
After the download is complete, run the installer and follow the instructions. In a terminal window, run the following command to verify that the installation was successful:
dotnet --version
You should see the version number of the .NET SDK you installed. For example:
8.0.300
Follow this Visual Studio 2022 Preview link, and select Download Preview. After the download is complete, run the installer and follow the instructions.
Follow this Docker Desktop link, and select the correct installer for your operating system. After the download is complete, run the installer and follow the instructions. For the best performance and compatibility, use the WSL 2 backend.
Open the Docker Desktop application and accept the service agreement.
Install the .NET Aspire workload using the .NET CLI:
Open a terminal.
Update .NET workloads with this command:
dotnet workload update
You should see a message that the workloads are updated successfully.
No workloads installed for this feature band. To update workloads installed with earlier SDK versions, include the --from-previous-sdk option.
Updated advertising manifest microsoft.net.sdk.ios.
Updated advertising manifest microsoft.net.workload.mono.toolchain.net6.
Updated advertising manifest microsoft.net.sdk.android.
Updated advertising manifest microsoft.net.workload.emscripten.net7.
Updated advertising manifest microsoft.net.workload.emscripten.net6.
Updated advertising manifest microsoft.net.sdk.macos.
Updated advertising manifest microsoft.net.workload.emscripten.current.
Updated advertising manifest microsoft.net.workload.mono.toolchain.current.
Updated advertising manifest microsoft.net.sdk.maui.
Updated advertising manifest microsoft.net.workload.mono.toolchain.net7.
Updated advertising manifest microsoft.net.sdk.maccatalyst.
Updated advertising manifest microsoft.net.sdk.tvos.
Updated advertising manifest microsoft.net.sdk.aspire.
No workloads installed for this feature band. To update workloads installed with earlier SDK versions, include the --from-previous-sdk option.
Successfully updated workload(s): .
Install the .NET Aspire workload with this command:
dotnet workload install aspire
You should see a message that the .NET Aspire workload has been installed.
Installing Aspire.Hosting.Sdk.Msi.x64 ...... Done
Installing Aspire.ProjectTemplates.Msi.x64 ..... Done
Installing Aspire.Hosting.Orchestration.win-x64.Msi.x64 ............. Done
Installing Aspire.Hosting.Msi.x64 ..... Done
Installing Aspire.Dashboard.Sdk.win-x64.Msi.x64 ....... Done
Successfully installed workload(s) aspire.
Verify that the .NET Aspire workload is installed with this command:
dotnet workload list
You should see the details of the .NET Aspire workload.
Installed Workload Id Manifest Version Installation Source
--------------------------------------------------------------------------------
aspire 8.0.1/8.0.100 SDK 8.0.300, VS 17.11.34929.205
Use `dotnet workload search` to find additional workloads to install.
Let's use git
to obtain a sample app to deploy:
In the command line, browse to a folder of your choice where you can work with code.
Execute the following command to clone the eShop sample application:
git clone -b aspire-azure-customize https://github.com/MicrosoftDocs/mslearn-aspire-starter
Open Visual Studio and then select Open a project or solution.
Browse to the folder where you cloned the sample solution, then in the start folder select eShop.azureresources.sln and then select Open.
To start the app in debugging mode press F5 or select Debug > Start Debugging.
If the Start Docker Desktop message appears, select Yes. The app starts and displays the .NET Aspire dashboard in a browser tab.
Notice that there's a container named redis. This container hosts the output cache for the webapp microservice.
Close the web browser tabs and in Visual Studio stop debugging.
When you deploy the eShop to Azure, you want to use Azure Cache for Redis to host the output cache, so that you don't have to manage a caching container. Let's set that up:
In Visual Studio, in the Solution Explorer, right-click the eShop.AppHost project, point to Add, and then select .NET Aspire package.
In the search textbox, at the end of the existing text, type redis.
Select Aspire.Hosting.Azure.Redis package.
In the Version list, select the latest 8.0.0 version, and then select Install.
If the Preview Changes dialog appears, select Apply.
In the License Acceptance dialog, select I Accept.
In the eShop.AppHost project, open the Program.cs file.
Locate the following line of code:
var redis = builder.AddRedis("redis");
Modify that line of code to match this line:
var redis = builder.AddRedis("redis")
.PublishAsAzureRedis();
Note
The solution already has code that passes the cache service to the web app project. The web app project uses it as an output cache for the Catalog.razor page. The changes you've made ensure that the deployed app will use Azure Cache for Redis, not a Redis container.
You want the deployed eShop to send telemetry data to Azure Application Insights. Let's start by configuring the app host project:
In Visual Studio, in the Solution Explorer, right-click the eShop.AppHost project, point to Add, and then select .NET Aspire package.
In the search textbox, at the end of the existing text, type insights.
Select Aspire.Hosting.Azure.ApplicationInsights package.
In the Version list, select the latest 8.0.0 version, and then select Install.
If the Preview Changes dialog appears, select Apply.
In the License Acceptance dialog, select I Accept.
In the eShop.AppHost project, open the Program.cs file.
Locate the following line of code:
var builder = DistributedApplication.CreateBuilder(args);
Immediately after that line, add the following line of code:
var insights = builder.AddAzureApplicationInsights("AppInsights");
Locate the following line of code, which adds the Catalog API project:
var catalogApi = builder.AddProject<Catalog_API>("catalog-api")
.WithReference(catalogDb);
To pass the Application Insights service to the Catalog API, modify that line of code to match this line:
var catalogApi = builder.AddProject<Catalog_API>("catalog-api")
.WithReference(catalogDb)
.WithReference(insights);
Locate the following line of code, which adds the Web App project:
builder.AddProject<WebApp>("webapp")
.WithReference(catalogApi)
.WithReference(redis);
To pass the Application Insights service to the Web App, modify that line of code to match this line:
builder.AddProject<WebApp>("webapp")
.WithReference(catalogApi)
.WithReference(redis)
.WithReference(insights);
The changes that you've made to the app host project deploy a new instance of Application Insights to Azure when you deploy the .NET Aspire solution. However, you must also configure the solution to send telemetry to that instance. Let's do that now:
In Visual Studio, in the Solution Explorer, right-click the eShop.ServiceDefaults project, and then select Manage NuGet packages.
Select the Browse tab.
In the search textbox, type Azure.Monitor.OpenTelemetry.
Select Azure.Monitor.OpenTelemetry.AspNetCode package, and then select Install.
If the Preview Changes dialog appears, select Apply.
In the License Acceptance dialog, select I Accept.
In Solution Explorer, expand the eShop.ServiceDefaults project and then double-click the HostingExtensions.cs file.
Locate the following code in the AddOpenTelemetryExporters
method:
if (useOtlpExporter)
{
builder.Services.AddOpenTelemetry().UseOtlpExporter();
}
Immediately after that code, add the following code:
if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
{
builder.Services.AddOpenTelemetry().UseAzureMonitor();
}
We'll use the Azure Developer CLI (azd
) to deploy our completed application:
In the command line, to install the Azure Developer CLI (azd
) run this command:
winget install microsoft.azd
Note
If you don't have winget
installed, see Use the winget tool to install and manage applications
To log into Azure, run this command, and then enter your Azure credentials:
azd auth login
Before you can deploy, use azd
to inspect the .NET Aspire solution and create manifest files:
In the command line, change into the directory where you cloned the sample application.
To navigate into the app host project, use this command:
cd Start\eShop.AppHost
To start the initialization, run this command:
azd init
azd
starts asks you how to initialize the app. Press Enter to use code in the current directory.
Initializing an app to run on Azure (azd init)
? How do you want to initialize your app? [Use arrows to move, type to filter]
> Use code in the current directory
Select a template
azd
scans the code in the current directory, detects .NET Aspire, and asks you to confirm that you want to continue. Press Enter:
Detected services:
.NET (Aspire)
Detected in: C:\Users\Admin\source\repos\eShop.AppHost\eShop.AppHost.csproj
azd will generate the files necessary to host your app on Azure using Azure Container Apps.
? Select an option [Use arrows to move, type to filter]
> Confirm and continue initializing my app
Cancel and exit
When azd
prompts you for a new environment name, type aspireeshop and then press Enter.
azd
generates the necessary files and completes the initialization:
Generating files to run your app on Azure:
(✓) Done: Generating ./azure.yaml
(✓) Done: Generating ./next-steps.md
SUCCESS: Your app is ready for the cloud!
You can provision and deploy your app to Azure by running the azd up command in this directory. For more information on configuring your app, see ./next-steps.md
Now that you have initialized azd
, you can continue with the deployment to Azure:
To start the deployment, run this command:
azd up
azd
analyzes the app and prompts you to choose an Azure subscription. Use the arrow keys to select the correct subscription and then press Enter:
? Select an Azure Subscription to use: [Use arrows to move, type to filter]
> 1. Pay-As-You-Go
azd
prompts you to choose an Azure location. Choose a location near you and then press Enter:
? Select an Azure location to use: [Use arrows to move, type to filter]
46. (US) East US STG (eastusstg)
47. (US) North Central US (northcentralus)
48. (US) South Central US (southcentralus)
49. (US) West Central US (westcentralus)
50. (US) West US (westus)
> 51. (US) West US 2 (westus2)
52. (US) West US 3 (westus3)
azd
provisions Azure resources. This process can take around 40 minutes. When the deployment is complete, a SUCCESS message appears:
Packaging services (azd package)
Provisioning Azure resources (azd provision)
Provisioning Azure resources can take some time.
Subscription: Pay-As-You-Go
Location: West US 2
You can view detailed progress in the Azure Portal:
https://portal.azure.com/#view/HubsExtension/DeploymentDetailsBlade/~/overview/providers%2FMicrosoft.Resources%2Fdeployments%2Faspireeshop
(✓) Done: Resource group: rg-aspireeshop
(✓) Done: Log Analytics workspace: law-u77k3ne4bclro
(✓) Done: Container Registry: acru77k3ne4bclro
(✓) Done: Key Vault: kvb6088994u77k3ne4bclro
(✓) Done: Container Apps Environment: cae-u77k3ne4bclro
(✓) Done: Application Insights: myapplicationinsightsu77
(✓) Done: Cache for Redis: redisu77k3ne4bclro
Deploying services (azd deploy)
(✓) Done: Deploying service catalog-api
- Endpoint: https://catalog-api.internal.proudpebble-4accbcad.westus2.azurecontainerapps.io/
(✓) Done: Deploying service catalog-db-mgr
- Endpoint: https://catalog-db-mgr.internal.proudpebble-4accbcad.westus2.azurecontainerapps.io/
(✓) Done: Deploying service postgres
- Endpoint: https://postgres.internal.proudpebble-4accbcad.westus2.azurecontainerapps.io/
(✓) Done: Deploying service webapp
- Endpoint: https://webapp.internal.proudpebble-4accbcad.westus2.azurecontainerapps.io/
Aspire Dashboard: https://aspire-dashboard.ext.proudpebble-4accbcad.westus2.azurecontainerapps.io
SUCCESS: Your up workflow to provision and deploy to Azure completed in 38 minutes 13 seconds.
When the deployment is complete, you can examine the new resources in your Azure subscription and test the deployed .NET Aspire solution:
Start a browser and navigate to the Azure portal
Sign into the subscription you chose in azd
.
In the portal home page, select Resource groups.
Select the rg-aspireeshop resource group. The resource group includes a container registry, a container apps environment, and four container apps. Select the webapp container app:
Select the webapp container app.
In the navigation on the left, under Settings, select Ingress.
Next to Ingress traffic, select Accept traffic from anywhere, and then select Save.
In the navigation on the left, select Overview, and then select the Application Url. The eShop homepage opens in a new browser tab.
Explore some products and brands in the eShop.
Switch back to the browser tab that displays the Azure portal and then browse to the rg-aspireeshop resource group.
In the list of resources, select the Application Insights instance.
The graphs on the home page show data about user requests. In the navigation on the left, under Investigate, select Application map. The map displays the PostgreSQL database, two container apps, and a Redis cache.
You can explore other displays in Application Insights. When you're finished, you can clean up your Azure subscription by deleting the rg-aspireeshop resource group.
Please sign in to use this experience.
Sign in