In this step, you'll orchestrate all apps using .NET Aspire.
- Install .NET SDK 9
- Install PowerShell 7
- Install Docker Desktop
- Install Visual Studio Code
To verify the installation of each prerequisite, refer to the document STEP 00: Setting Up the Development Environment.
You can continue using the app from the previous step or copy a fresh version from the save point using the commands below. To copy a fresh version, use the following commands:
-
Open a terminal and execute the following commands sequentially to create the practice directory and copy the base project:
# Bash/Zsh REPOSITORY_ROOT=$(git rev-parse --show-toplevel) cd $REPOSITORY_ROOT mkdir -p workshop && cp -a save-points/step-00/. workshop/
# PowerShell $REPOSITORY_ROOT = git rev-parse --show-toplevel cd $REPOSITORY_ROOT New-Item -Type Directory -Path workshop -Force && Copy-Item -Path ./save-points/step-00/* -Destination ./workshop -Recurse -Force
-
Build the entire project using the following command:
cd $REPOSITORY_ROOT/workshop dotnet restore && dotnet build
Unlike the previous practice, this time you will use .NET Aspire for container orchestration.
Once you install the .NET Aspire orchestration project, the overall solution structure will change as shown below:
eShopLite
└── src
├── eShopLite.AppHost
│ ├── eShopLite.WebApp
│ ├── eShopLite.ProductApi
│ └── eShopLite.WeatherApi
├── eShopLite.ServiceDefaults
├── eShopLite.WebApp
│ ├── eShopLite.DataEntities
│ └── eShopLite.ServiceDefaults
├── eShopLite.WeatherApi
│ ├── eShopLite.DataEntities
│ └── eShopLite.ServiceDefaults
└── eShopLite.ProductApi
├── eShopLite.ProductData
│ └── eShopLite.DataEntities
└── eShopLite.ServiceDefaults
-
Run the following command to add the .NET Aspire orchestrator project:
dotnet new aspire-apphost -n eShopLite.AppHost -o src/eShopLite.AppHost dotnet sln eShopLite.sln add ./src/eShopLite.AppHost
-
Execute the following command to add all apps to the .NET Aspire orchestrator project:
dotnet add ./src/eShopLite.AppHost reference ./src/eShopLite.WebApp dotnet add ./src/eShopLite.AppHost reference ./src/eShopLite.ProductApi dotnet add ./src/eShopLite.AppHost reference ./src/eShopLite.WeatherApi
-
Use the command below to add the .NET Aspire base service project:
dotnet new aspire-servicedefaults -n eShopLite.ServiceDefaults -o src/eShopLite.ServiceDefaults dotnet sln eShopLite.sln add ./src/eShopLite.ServiceDefaults
-
Run the following command to add the .NET Aspire base service project to each app:
dotnet add ./src/eShopLite.WebApp reference ./src/eShopLite.ServiceDefaults dotnet add ./src/eShopLite.ProductApi reference ./src/eShopLite.ServiceDefaults dotnet add ./src/eShopLite.WeatherApi reference ./src/eShopLite.ServiceDefaults
-
Open
src/eShopLite.WebApp/Program.cs, findvar builder = WebApplication.CreateBuilder(args);and add the following content immediately below:builder.AddServiceDefaults();
This enables the use of default services provided by .NET Aspire.
-
Update the following code:
// Before builder.Services.AddHttpClient<ProductApiClient>(client => { client.BaseAddress = new("http://localhost:5051"); }); builder.Services.AddHttpClient<WeatherApiClient>(client => { client.BaseAddress = new("http://localhost:5050"); });
// After builder.Services.AddHttpClient<ProductApiClient>(client => { client.BaseAddress = new("https+http://productapi"); }); builder.Services.AddHttpClient<WeatherApiClient>(client => { client.BaseAddress = new("https+http://weatherapi"); });
Modify to use service discovery provided by .NET Aspire.
-
Add the following content right before
app.Run();:app.MapDefaultEndpoints();
Enables the use of the health check endpoint provided from .NET Aspire.
-
Open
src/eShopLite.ProductApi/Program.cs, findvar builder = WebApplication.CreateBuilder(args);and add the following content immediately below:builder.AddServiceDefaults();
-
Add the following line right before
app.Run();:app.MapDefaultEndpoints();
🚨🚨🚨 도전
‼️ 🚨🚨🚨Like modifying the
eShopLite.ProductApiproject, modify theeShopLite.WeatherApiproject.
-
Open
src/eShopLite.AppHost/Program.cs, findvar builder = DistributedApplication.CreateBuilder(args);and add the following lines immediately below:var productapi = builder.AddProject<Projects.eShopLite_ProductApi>("productapi"); var weatherapi = builder.AddProject<Projects.eShopLite_WeatherApi>("weatherapi");
Add both
ProductApiandWeatherApito the orchestrator project,AppHost. -
Add the following content in the subsequent line:
builder.AddProject<Projects.eShopLite_WebApp>("webapp") .WithExternalHttpEndpoints() .WithReference(productapi) .WithReference(weatherapi) .WaitFor(productapi) .WaitFor(weatherapi);
Configure the
WebAppproject in the orchestrator,AppHost.WithExternalHttpEndpoints(): exposes for the public access..WithReference(productapi): letsWebAppdiscoverProductApi..WithReference(weatherapi): letsWebAppdiscoverWeatherApi..WaitFor(productapi): letsWebAppwait forProductApibeing up and running..WaitFor(weatherapi): letsWebAppwait forWeatherApibeing up and running.
-
Execute the following command to run the .NET Aspire orchestrator:
cd $REPOSITORY_ROOT/workshop dotnet watch run --project ./src/eShopLite.AppHost
-
A web browser will automatically open, displaying the dashboard. The dashboard will show
productapi,weatherapiandwebappresources.You might be seeing this login screen.
Click the link and follow the instructions to get into the dashboard.
-
Click each endpoint of
productapiandweatherapito see their respective OpenAPI document. -
Click the enpoint of
webappto see the web application. Navigate to both/productsand/weatherpages and see whether they are properly up. -
Type
Ctrl+Cin the terminal and stop the .NET Aspire orchestrator.
Let's change the database from SQLite to PostgreSQL.
-
Run the following command to add the PostgreSQL package to the
eShopLite.AppHostproject.cd $REPOSITORY_ROOT/workshop dotnet add ./src/eShopLite.AppHost package Aspire.Hosting.PostgreSQL
-
Open
src/eShopLite.AppHost/Program.cs, findvar builder = DistributedApplication.CreateBuilder(args);and add the following content immediately below:var productsdb = builder.AddPostgres("pg") .WithPgAdmin() .AddDatabase("productsdb");
It adds a PostgreSQL database.
.AddPostgres("pg"): adds a container for PostgreSQL database..WithPgAdmin(): adds a container for PGAdmin dashboard..AddDatabase("productsdb"): adds a new database calledproductsdb.
-
Modify the following content:
// Before var productapi = builder.AddProject<Projects.eShopLite_ProductApi>("productapi");
// After var productapi = builder.AddProject<Projects.eShopLite_ProductApi>("productapi") .WithReference(productsdb);
It adds the PostgreSQL database to the
ProductApiproject..WithReference(productsdb): letProductApidiscover the PostgreSQL database,productsdb.
-
Run the following command to add a PostgreSQL database packaget to the
eShopLite.ProductApiproject.cd $REPOSITORY_ROOT/workshop dotnet add ./src/eShopLite.ProductApi package Aspire.Npgsql.EntityFrameworkCore.PostgreSQL
-
Open
src/eShopLite.ProductApi/appsettings.json, remove theConnectionStringssection completely. Theappsettings.jsonfile should look like:{ "Logging": { "LogLevel": { "Default": "Information", "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" }Make sure there's no
ConnectionStringssection any longer. -
Open the
src/eShopLite.ProductApi/Program.csfile and modify it as follows:// Before builder.Services.AddDbContext<ProductDbContext>(options => { var connectionString = builder.Configuration.GetConnectionString("ProductsContext") ?? throw new InvalidOperationException("Connection string 'ProductsContext' not found."); options.UseSqlite(connectionString); });
// After builder.AddNpgsqlDbContext<ProductDbContext>("productsdb");
Update to use the PostgreSQL database connection string.
- The name
productsdbis the reference name thatAppHostuses.
- The name
-
Execute the following command to run the .NET Aspire orchestrator:
cd $REPOSITORY_ROOT/workshop dotnet watch run --project ./src/eShopLite.AppHost
-
A web browser will automatically open, displaying the dashboard. The dashboard will show
pg,pg-pgadmin,productsdb,productapi,weatherapiandwebappresources.You might be seeing the login screen.
Click the link and follow the instructions so that you can access to the dashboard.
-
Click the endpoint of
pg-pgadminto see the admin dashboard for the PostgreSQL database. -
Click the endpoint of both
productapiandweatherapito see respective OpenAPI document. -
Click the endpoint of
webappto see the web app. Navigate to both/productsand/weatherpages to see they are properly showing up. -
Type
Ctrl+Cin the terminal to to stop the .NET Aspire orchestrator.
Congratulations! You've completed the Container Orchestration with .NET Aspire practice. Proceed to the next step: STEP 05: Integration Testing with .NET Aspire.
Disclaimer:
This document has been translated using machine-based AI translation services. While efforts are made to ensure accuracy, please note that automated translations may contain errors or inaccuracies. The original document in its native language should be regarded as the authoritative source. For critical information, professional human translation is advised. We are not responsible for any misunderstandings or misinterpretations resulting from the use of this translation.


