Skip to content

feat(aspire): bake service addresses into WebAssembly and Android apps - #3311

Open
Kunal22shah wants to merge 12 commits into
dev/ks/aspire-apphostfrom
dev/ks/aspire-baking
Open

Kunal22shah wants to merge 12 commits into
dev/ks/aspire-apphostfrom
dev/ks/aspire-baking

Conversation

@Kunal22shah

@Kunal22shah Kunal22shah commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Stack 3 of 4. Based on #3310 (AppHost package), which is based on #3309 (client package); next: #3317 (dev server for Hot Reload).

PR Type

  • Feature

What is the current behavior?

The WebAssembly and Android heads run, but their apps don't get the API's address: a browser has no process environment, and an Android app doesn't inherit the one dotnet run had.

What is the new behavior?

Before a WebAssembly or Android head is built, the AppHost writes the addresses of the services it references into the app's obj/uno-aspire folder. Build logic shipped in Uno.Extensions.Aspire reads them into the app, in Debug builds only:

  • WebAssembly: as bootstrapper environment (WasmShellMonoEnvironment).
  • Android: as the same items dotnet run -e adds (RuntimeEnvironmentVariable). A localhost address becomes 10.0.2.2, over http only, and names are uppercased.

Safety:

  • Allow-list. Only service addresses are written. Connection strings, secrets and other values are left out, and the head's log says which. WithClientEnvironment("Name") opts one variable in on purpose. If the environment fails to resolve, nothing is written.
  • Never in a shipped build. Release builds and publishes never read the file. The AppHost deletes it when it starts and when it stops.
  • Never after the AppHost is gone. The AppHost renews the file every minute. A file left by a killed AppHost, as an IDE's stop does, is removed by the next build once it is five minutes old (UNOASPIRE103), instead of baking addresses nobody serves.

Every build reads it, IDE builds included. Because the file is in the app's own folder, building in your IDE while the head runs keeps the address.

Warnings point at the cause when the app gets no address:

  • the app doesn't reference Uno.Extensions.Aspire;
  • the two packages are at different versions;
  • the Android SDK didn't write the values (UNOASPIRE102; the package version mismatch is UNOASPIRE101);
  • the AppHost that wrote the values stopped renewing them (UNOASPIRE103);
  • a service offered the emulator only https.

Reviewer's guide

  • Start with the spec's Baking the environment, WebAssembly and Android emulator sections.
  • AppHost side:
    • BakedEnvironment.cs: the allow-list (Plan), the Android rules (ForAndroidEmulator) and writing the file.
    • ClientEnvironment.cs: writing the file before the build, removing it with the AppHost, and the head's log.
  • App side: src/Uno.Extensions.Aspire/buildTransitive/Uno.Extensions.Aspire.targets.
  • Tests:
    • Given_WebAssemblyHead, Given_AndroidEmulatorHead: application model.
    • Given_AppBuildLogic: runs the real build logic with MSBuild against what the AppHost wrote; Debug, Release, publish, other configuration, other version, Android SDK that ignores the values.
  • The last commit is docs only: the how-to doc/Learn/Http/HowTo-Aspire.md for the whole feature (desktop, WebAssembly, Android) and its TOC entry. The spec is in feat(aspire): add the Uno.Extensions.Aspire client package #3309.

Verified on the sample:

  • WebAssembly: the browser called the API at the baked address, over http and over https. An IDE-style dotnet build during the run kept the address.
  • Android emulator (API 35): a task added on the emulator reached the API and the database. The network security configuration allows plain http to 10.0.2.2 only.
  • After the AppHost stops, the next build carries no address.

PR Checklist

  • Tested code with current supported SDKs
  • Docs have been added/updated
  • Unit Tests and/or UI Tests for the changes have been added
  • Wasm UI Tests are not showing unexpected differences
  • Contains NO breaking changes
  • Updated the Release Notes
  • Associated with an issue

Other information

  • The Android hook relies on a .NET for Android target that is not public. It was verified on SDK 36.1.118, and a build warning covers a later rename.
  • Not verified: several emulators or a device attached at once, and two heads building the same project at once.

🤖 Generated with Claude Code

@Kunal22shah
Kunal22shah marked this pull request as ready for review November 18, 2026 19:42
@mergify

mergify Bot commented Nov 18, 2026

Copy link
Copy Markdown

Tick the box to add this pull request to the merge queue (same as @mergifyio queue). If it does not match the queue conditions yet, it enters the queue once it does.

  • Queue this pull request

@carldebilly carldebilly left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The baking design is sound for a first version, and safer than what Aspire.Hosting.Maui does: only service addresses are written (MAUI writes every variable, OTLP headers included), they are read in Debug only, and every warning names its cause. Hooking _GenerateEnvironmentFiles with RuntimeEnvironmentVariable is the same hook MAUI relies on, so no objection there. The overall review of the stack is on #3309.

Before merge:

  • Public naming. WithBakedEnvironment names the mechanism. For WebAssembly, the longer-term direction is runtime delivery, as Aspire.Hosting.Blazor does with its gateway (configuration read before boot, APIs proxied same-origin, OTLP proxied), which needs a pre-boot hook in Uno.Wasm.Bootstrap. A mechanism-neutral name such as WithClientEnvironment keeps that door open without another rename. Knock-on: both overloads, the head log messages that cite it, the how-to and the spec.
  • Android SDK floor. RuntimeEnvironmentVariable needs the .NET 10.0.300 SDK; the how-to should state it next to the existing warning.

Later (fine as follow-ups):

  • Dev tunnels as a documented, tested path for the emulator and devices, which is what lets MAUI drop 10.0.2.2 and the hand-written network security configuration.
  • A values file outside obj (e.g. a temp folder keyed by the project path), so a custom BaseIntermediateOutputPath or artifacts output does not leave a stray folder and a killed AppHost cannot leave addresses behind.

Nits:

  • UnoAppBuilderExtensions.cs:251-255 copies the environment into a List of unnamed tuples only to pass it as an IEnumerable; a Select into named tuples does the same.
  • UnoAppBuilderExtensions.cs:295-324: string.Join runs for the warnings without an IsEnabled guard (AGENTS.md); passing the lists as arguments lets the logger join them.
  • ProjectAssets.cs:11: "libraries"u8 instead of Encoding.UTF8.GetBytes("libraries").
  • BakedEnvironment.cs:213: the upper-cased prefix is rebuilt for every pair; hoist it per service.

Comment thread doc/Learn/Http/HowTo-Aspire.md Resolved
Comment thread doc/Learn/Aspire/HowTo-Aspire.md Resolved
Comment thread doc/Learn/Http/HowTo-Aspire.md Resolved

@kazo0 kazo0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before merging: see the CI note on #3310. The pipeline hasn't run on this branch either.

@kazo0 kazo0 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Validated the stack end to end on Windows, using local packages from dev/ks/aspire-baking (a312556) in a fresh dotnet new unoapp (Kiota, desktop + WebAssembly + Android) with an ASP.NET Core API and an Aspire 13.6.5 AppHost, driven through the Aspire CLI 17.

  • Client (#3309):
    • Discovery resolves from the environment, and Aspire's address wins over a services section in settings.
    • Against a 503 endpoint: GET reaches the API 4 times, POST once. With UseResilience = false, Kiota's own retries send POST 4 times.
    • Telemetry is exported in Debug/Development and not in Release/Production, checked against a fake OTLP receiver.
    • DiagnosticHandler shows Authorization: [redacted] and no query string.
  • AppHost (#3310):
    • A second head waits while another builds ("Waiting for another head of 'app' to finish building").
    • The target-framework error is clear.
    • WithArgs properties reach the build.
    • Stopping a head closes the app (desktop and Android). Killing the AppHost left no processes behind.
  • Values written into WebAssembly and Android (#3311):
    • Only service addresses are written. The secret is left out and the log names it; WithClientEnvironment opts a variable in.
    • The WebAssembly app in headless Edge called the API over https, and the CORS preflight passed.
    • Android (API 36 emulator, workload 37.2.31) called the API over http via 10.0.2.2, with uppercase names.
    • An IDE-style build keeps the values, a Release publish carries none, and a clean stop removes the file.
    • The lease renews every minute; a killed AppHost's file is removed with UNOASPIRE103.
    • An https-only API gives the emulator warning.

Not covered: Hot Reload (in progress), WebAssembly over plain http, macOS/Linux, physical devices, AOT.

Comment thread doc/Learn/Http/HowTo-Aspire.md Resolved
@kazo0

kazo0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Follow-up: Hot Reload also checked, on the desktop head started by the AppHost, with uno.devserver running. A XAML edit (MainPage.xaml) and a C# edit (MainPage.xaml.cs) both reached the running window, with no restart of the app process. So DOTNET_MODIFIABLE_ASSEMBLIES=debug set by the head (#3310) works as intended. With the template's CoreLogLevel(Warning), a successful update logs nothing in the head's log; only failures would show.

@kazo0

kazo0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Docs check for doc/Learn/Http/HowTo-Aspire.md:

  • Not checked in CI yet. The doc checks (cspell and markdownlint, build/ci/stage-docs-validations.yml) run in the Azure uno.extensions pipeline, which is skipped on this PR because its base isn't main. I ran both locally with the repo's configs (build/ci/cspell.json, build/ci/.markdownlint.json) on the how-to, toc.yml and the spec: no issues. Both cross-references (HowToKiota, HowToEndpointOptions) and the three in-page anchors resolve, and the toc entry points at the page's uid. They'll run for real once this PR is retargeted to main.

  • Only the toc links to the page. Suggest adding it to the References list in doc/Learn/Http/HttpOverview.md, next to the other how-tos:

    - [How-To: Run an app with its backend under Aspire](xref:Uno.Extensions.Http.HowToAspire)

    Optionally, add a one-line mention under the Kiota section of the same page, since UseServiceDefaults() changes how Kiota retries.

Not blocking.

@kazo0

kazo0 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

macOS validation: values written into WebAssembly builds

Packages 255.255.255.255-aspire.1 built from dev/ks/aspire-baking @ a312556 (the same bits as the Windows run). Environment: macOS (Darwin 27.2, arm64), .NET SDK 10.0.403, Xcode 27.1, AppHost SDK 13.6.5, Aspire CLI 13.6.4 (check 8 repeated with 17.0.0).

Everything matches the Windows baseline.

# Check Result
16 Only service addresses written ✅ services__api__*, ASPIRETEST_PUBLIC and ASPIRETEST_PROBE are written. Not written into the app 'app-wasm' … API_HTTP, API_HTTPS, ASPIRETEST_SECRET.
15 WebAssembly calls the API (headless Chrome) ✅ ping GET https = 1, flaky GET https = 4, flaky POST https = 1 (CORS preflight passed)
18 IDE build keeps the values ✅ "written into the app" line, both addresses in uno-config.js, no secret
17 Never in Release/publish ✅ no address in the published output, including wwwroot/uno-config.js
19 Removed on clean stop ✅ obj/uno-aspire is empty
20 Stale file after a killed AppHost ✅ the file is renewed every ~60 s. After kill -9 and backdating it 6 min: UNOASPIRE103, file removed, no address in uno-config.js
21 UNOASPIRE101 ✅ fires on a v2 file (the how-to wording point already reported here still applies)

Differences from Windows:

  • After kill -9 of the AppHost and CLI in check 20, no processes were left running. DCP (started with --monitor <AppHost PID>) stopped the API and the heads itself.

Android (23–25) wasn't run on the Mac.

Comment thread doc/Learn/Aspire/HowTo-Aspire.md Resolved
Comment thread doc/Learn/Aspire/HowTo-Aspire.md Resolved
Comment thread doc/Learn/Aspire/HowTo-Aspire.md Resolved
@Kunal22shah

Copy link
Copy Markdown
Contributor Author

Thanks for the docs check. Done in 1455bcb: the how-to is now linked from the References list in doc/Learn/Http/HttpOverview.md. It also moved to its own Aspire section (9957cbf), so its uid is now Uno.Extensions.Aspire.HowToAspire.

I left out the Kiota note in the overview: the how-to already covers how UseServiceDefaults() changes retries.

…and Android apps

- before a WebAssembly or Android head is built, the AppHost writes its allow-listed values
  into the app's obj/uno-aspire; nothing is written when the environment fails to resolve
- Uno.Extensions.Aspire ships the build logic that reads them in Debug builds, IDE builds included:
  WasmShellMonoEnvironment for WebAssembly, RuntimeEnvironmentVariable for Android
- Android: loopback service addresses get 10.0.2.2 over http only; names uppercased
- WithClientEnvironment opts a variable in; the head's log names what was written or left out
- the AppHost removes the file at its start and stop; build warnings UNOASPIRE101 and UNOASPIRE102
  name a format or Android SDK mismatch
- build tests run that logic with MSBuild
- the AppHost renews each values file every minute while it runs
- a Debug build ignores a file older than five minutes, with warning UNOASPIRE103, instead of baking dead addresses
- The check read the restore from before the head's build, so a reference changed by a WithArgs property warned one start late
- Install through the Aspire UnoFeature, with the package reference for older SDKs

- Require the Http feature only

- UNOASPIRE101 is about the file format, not the package versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants