Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
---
license: MIT
name: add-aspire-to-blazor
description: >-
Add or complete .NET Aspire integration for an existing browser-hosted Blazor
WebAssembly app or ASP.NET Core Blazor Web App. DO NOT USE FOR: .NET MAUI,
Blazor Hybrid, BlazorWebView, or native-process telemetry, even if the request
mentions Aspire or the Aspire dashboard. USE FOR: "add Aspire to my Blazor app",
wiring a standalone WebAssembly gateway or a Blazor Web App into an AppHost,
configuring service discovery and same-origin API proxying, or exporting
OpenTelemetry from these web apps. Also exclude creating a new Blazor app
(use create-blazor-project), general HTTP data loading (use fetch-and-send-data),
non-Blazor apps,
Azure/Kubernetes deployment, or AppHost databases/containers/messaging unrelated to
the Blazor front end.
compatibility: Requires .NET 11.0 or later and Aspire 17.0 or later. The browser recipes require Aspire.Hosting.Blazor.
---

# Add .NET Aspire to a Blazor App

Connect an existing Blazor front end to backend services through **Aspire service discovery** and stream its telemetry — including **browser telemetry** from a WebAssembly client — to the **Aspire dashboard**.

## Prerequisites

Require **.NET 11.0 or later** for the Blazor app and **Aspire 17.0 or later**. From the
solution directory, run `dotnet --version` and `aspire --version`. Inspect `global.json`,
the Blazor target frameworks, and the AppHost package versions. Check the version and
runtime required by the existing AppHost separately from the Blazor app's target.
Preserve working versions and package sources. If the project falls below the minimum,
report what must change and get agreement before upgrading.

Use `--prerelease` when selecting `Aspire.Hosting.Blazor` for the browser recipes.
If the project requires stable-only packages, skip that integration and
report which browser capabilities it blocks. Use stable Aspire packages for server-only
orchestration.

If the user asks for advice only, describe the applicable commands and edits without
executing them. Mark runtime checks as not run.

## Workflow

### Step 1 — Choose the Blazor path

Inspect the app's project file, startup code, and render modes. Keep the existing render mode.

| If you find | Do this |
|---|---|
| `Microsoft.NET.Sdk.BlazorWebAssembly`, `wwwroot/index.html`, and no server host | Open [standalone-gateway.md](references/standalone-gateway.md). Serve the client through a Blazor Gateway. |
| `Microsoft.NET.Sdk.Web` with `AddRazorComponents().AddInteractiveWebAssemblyComponents()` and `MapRazorComponents<App>().AddInteractiveWebAssemblyRenderMode()` | Open [hosted-webapp.md](references/hosted-webapp.md). Keep the existing server as the browser host and configure its YARP proxy. |
| A Blazor Web App that uses only static SSR or Interactive Server | Configure the AppHost, server defaults, and server-side API discovery. Skip the browser steps. Do not add a `.Client` project or a gateway. |

Resolve links relative to this `SKILL.md`. If a reference is missing, list `references`
once and report which instructions you could not verify.

### Step 2 — Report capabilities and actions before editing

Write a `Capabilities` table with columns `Status`, `Capability`, and `Finding`. Use
✅ **Present**, ❌ **Missing**, — **Not applicable**, or ⚠️ **Blocked**. For each item
below, name the observed version, project, registration, or configuration that supports
the status. Do not mark runtime behavior as verified from source alone.

| Capability | Check |
|---|---|
| .NET and Aspire | Record each detected version and whether it meets the minimum. |
| AppHost | Report project-based or single-file, and whether it exists or is selected for creation. If it exists, check for `DistributedApplication.CreateBuilder` and its AppHost package or SDK. |
| Resource registration | Check that the AppHost registers each relevant app, gateway, and managed API. |
| Blazor hosting | Report the path selected in Step 1. |
| API dependencies | List each as AppHost-managed or third-party. If the app calls no APIs, report `none`. |
| Server defaults | Check each hosted server and owned API for `AddServiceDefaults()` before `Build()` and `MapDefaultEndpoints()` before `Run()`. Reuse an existing defaults library. |
| WebAssembly defaults | Find the client service-defaults library, confirm the WASM project references it, and check startup for `AddBlazorClientServiceDefaults()`. Check that the library matches the hosting path in Step 1. |
| Browser hosting and configuration | Check the standalone gateway, base path, and client configuration, or the hosted server's `WithBrowserOptions` wiring. |
| API discovery | Check that the app references each managed API resource in the AppHost and the applicable HTTP clients use its resource name, not a literal backend host or port. If a hosted component runs on both server and browser, check that both register the same named or typed client. |
| Browser API proxy | Check for a gateway reference on the standalone path or a mapped YARP route on the hosted path. Confirm the browser gets the proxy address through configuration. |
| Server telemetry | Check exporter configuration for each applicable server resource. |
| Browser telemetry | Check that the dashboard exposes an HTTP/protobuf OTLP receiver and that the browser gets a same-origin OTLP path. Do not count the server's exporter as browser telemetry. |

Apply these rules before selecting actions:

- If no APIs exist, mark API discovery and API proxying **Not applicable**. Keep the
applicable hosting and telemetry steps. If only third-party APIs exist, preserve their
URLs and access policies. Do not register them for Aspire discovery or proxy them by
default. Configure discovery for AppHost-managed APIs even when there are no third-party APIs.
- If the app does not run WebAssembly, mark browser-only capabilities **Not applicable**.
- Under `Actions`, number only the changes needed for **Missing** items. Name each
file relative to the solution root. For commands, name the working directory and
show the resolved command. For **Blocked** items, name the prerequisite or decision
instead of scheduling the blocked action. If no changes are needed, write `Actions: none`.

Replace each `<<placeholder-name>>` before running a command. Resolve path placeholders
relative to the stated working directory. Run the actions in order, then
verify the applicable capabilities in Step 5.

### Step 3 — Add the AppHost and server defaults

Run the commands in this step from the solution root.

1. If there is no AppHost, run `dotnet new aspire-apphost -o "<<apphost-directory>>"` for a project-based
host or `dotnet new aspire-apphost-singlefile -o "<<apphost-directory>>"` for a file-based host. If a
template is missing, run `dotnet new install "Aspire.ProjectTemplates::<<aspire-version>>"`
with the matching template version. Keep an existing AppHost's format.
2. If the hosted server or an owned API lacks server defaults, reuse the solution's
defaults library. If none exists, run `dotnet new aspire-servicedefaults -o "<<server-defaults-directory>>"`.
Add a reference from each server project that needs it, substituting the actual paths:

```bash
dotnet add "<<server-project-path>>" reference "<<server-defaults-project-path>>"
```

Call `builder.AddServiceDefaults()` before `Build()` and `app.MapDefaultEndpoints()`
before `Run()`. Do not reference server defaults from a WebAssembly project.
3. Register missing hosted servers and owned APIs with `AddDotnetProject`, using paths
relative to the AppHost. If the hosted server calls an owned API, add a reference
from the server to the API resource:

```csharp
var api = builder.AddDotnetProject("your-api", "../Api/Api.csproj");
builder.AddDotnetProject("webapp", "../WebApp/WebApp.csproj").WithReference(api);
```

Keep legacy `Projects.*` references used by other resources.
4. Configure server HTTP clients that call an AppHost-managed API to use the API
resource name. Use an `https` discovery URI for HTTPS-only or `https+http` if HTTP
fallback is allowed. Keep working registrations and transport policy.

### Step 4 — Add the missing WebAssembly wiring

1. Skip this step if the app does not run WebAssembly or cannot use the Blazor hosting
package. Otherwise, follow [the shared client setup](references/wasm-client.md) to
generate or reuse client service defaults and wire them in the client's `Program.cs`.
Register resource-named HTTP clients only for AppHost-managed APIs. Reuse the same
registration on the hosted server if the component also calls the API there.
2. Follow the reference selected in Step 1. For standalone, register the client with
`AddBlazorWasmApp`, add `WithReference(api)` if it calls an AppHost-managed API,
attach it to the gateway with `WithBlazorClientApp`, and match `index.html`'s
`<base href>` to the resource sub-path. For hosted, map the
AppHost-generated proxy routes with YARP, and pass browser configuration using
`WithBrowserOptions`. Keep proxy setup and configuration parsing in a separate
helper file; call the helpers from `Program.cs`. For AppHost-managed APIs, route
browser requests through the app origin and verify protected routes still enforce
authorization.
3. Check that the browser receives an OTLP path. If Aspire cannot resolve a dashboard
HTTP/protobuf receiver, configure it in the active AppHost profile using
[dashboard endpoint troubleshooting](references/troubleshooting.md#missing-browser-otlp-endpoint).
Do not send dashboard addresses or credentials to the browser.

### Step 5 — Run the AppHost and verify each signal

From the AppHost directory, use the existing HTTPS profile and run:

```bash
aspire run
```

1. In a second terminal in the AppHost directory, run `aspire describe --format Json`.
Check resource states and take the app or gateway HTTPS URL from the output. For
standalone, check the [gateway configuration](references/standalone-gateway.md#validate)
before opening a browser.
2. Open the app and trigger one interaction. Use the browser's Network panel or, if
installed, `playwright-cli` in one named session:

```shell
playwright-cli -s=blazor-validation open "<<app-url>>"
playwright-cli -s=blazor-validation snapshot
playwright-cli -s=blazor-validation requests
```

Use `goto` for a deep link and `click` with an element reference from `snapshot` if
needed. Confirm visible app content and inspect the statuses of framework assets,
any applicable same-origin API request, and the browser's OTLP posts. Use the
[standalone](references/standalone-gateway.md#validate) or
[hosted](references/hosted-webapp.md#validate) checks for the exact paths.
3. Use Aspire CLI to confirm dashboard ingestion for each applicable server or browser
telemetry source. Use the actual source name, not the gateway name for browser telemetry:

```shell
aspire otel logs "<<telemetry-source>>" --limit 5
aspire otel spans "<<telemetry-source>>" --limit 5 --format Json
```

If the app calls a managed API, take the trace ID from the relevant request span and
run `aspire otel spans --trace-id "<<trace-id>>"` to check the app and API spans together.
4. Keep the page open through one metric export interval if checking browser metrics.
Aspire 17's CLI does not show metrics. To verify dashboard display, open **Metrics**
in the dashboard and select the emitting resource. If you only observe a successful
metrics POST, report **export accepted; dashboard display not checked**. Do not
share the dashboard login token. Close the Playwright session if you opened it:
`playwright-cli -s=blazor-validation close`.

Write a `Verification` table with columns `Check`, `Status`, and `Evidence`. Mark each
applicable check **Passed**, **Failed**, or **Not run**. Cite the observed result or
explain why the check could not run. Do not report browser telemetry as verified from
a successful build or server-only trace.

If a check fails, open [troubleshooting.md](references/troubleshooting.md) and follow
the entry for the observed symptom.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": "0.1.3",
"category": "Web",
"compatibility": "Requires a Blazor project or solution.",
"package_prefix": "Microsoft.AspNetCore.Components"
}
Loading