This post walks through how to orchestrate Keycloak, Platform, and Portal applications using .NET Aspire — complete with OpenTelemetry integration, configurable RUN_MODE, and a flexible multi-project structure that scales from infra-only to a full stack.
1. Setting up the Infra layer
Start by preparing your .NET and Aspire projects:
# Set SDK version
dotnet new globaljson --sdk-version 9.0.304
# Aspire orchestration projects
dotnet new aspire-apphost -n AppHost -o infra/aspire/AppHost -f net9.0
dotnet new aspire-servicedefaults -n ServiceDefaults -o infra/aspire/ServiceDefaults -f net9.0
2. Backend services
We’ll define two web APIs — Platform and Portal — both using shared authentication logic via Keycloak.
dotnet new webapi -n Platform -o services/backend/Platform -f net9.0 --use-controllers
dotnet new webapi -n Portal -o services/backend/Portal -f net9.0 --use-controllers
dotnet add services/backend/Platform/Platform.csproj reference infra/aspire/ServiceDefaults/ServiceDefaults.csproj
dotnet add services/backend/Portal/Portal.csproj reference infra/aspire/ServiceDefaults/ServiceDefaults.csproj
dotnet add infra/aspire/AppHost/AppHost.csproj reference services/backend/Platform/Platform.csproj
dotnet add infra/aspire/AppHost/AppHost.csproj reference services/backend/Portal/Portal.csproj
# Shared authentication library
mkdir -p services/backend/_shared/Common.Auth
dotnet new classlib -n Common.Auth -f net9.0 -o services/backend/_shared/Common.Auth
dotnet add services/backend/Platform/Platform.csproj reference services/backend/_shared/Common.Auth/Common.Auth.csproj
dotnet add services/backend/Portal/Portal.csproj reference services/backend/_shared/Common.Auth/Common.Auth.csproj
3. RUN_MODE: controlling what to launch
Your RUN_MODE variable defines which part of the system Aspire starts.
Examples:
| Mode | Description |
|---|---|
infra-only | Only observability + databases + Keycloak |
platform:be | Platform backend only |
platform:be+fe | Platform backend + frontend |
platform:be,portal:be+fe | Platform backend + Portal stack |
platform:be+fe,portal:be+fe | Full stack |
