-
Notifications
You must be signed in to change notification settings - Fork 854
Add Aspire.Hosting.Blazor integration for Blazor WebAssembly apps #15691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
javiercn
wants to merge
25
commits into
microsoft:main
Choose a base branch
from
javiercn:javiercn/aspire-blazor-host
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
d2600b2
Aspire with Blazor sample
javiercn 1401f49
Traces working
javiercn fd8866d
Logs working
javiercn 72b3dd7
Metrics working
javiercn bebece7
Separate client telemetry from server telemetry
javiercn 5850ba8
Cleanup
javiercn 2325030
Cleanup
javiercn 3d1eed2
generalize config
javiercn 74d9bec
cleanups
javiercn 5d440e0
Remove comments and console.writeline/log
javiercn 2a257fd
Cleanups
javiercn a1af738
Aspire with Standalone Blazor
javiercn 8aeeb63
Aspire with Standalone Blazor working
javiercn 26b5518
Add readme
javiercn 708f6e5
Add proxy support
javiercn dc741d5
tmp
javiercn de9d459
Proxy telemetry
javiercn 17feaa3
Add Aspire.Hosting.Blazor integration for standalone Blazor WASM apps
javiercn 48ab3f4
Emit __ separators directly in client config JSON; add AspireExportIg…
javiercn bae4bfd
Add ProxyService/ProxyTelemetry hosted WASM APIs, tests, and hosted p…
javiercn 7d3a810
Fix hosted telemetry: DOM comment config, Lazy<T> reentrancy, namespa…
javiercn c223312
Update standalone playground README with accurate APIs and Mermaid di…
javiercn 213b47a
Add hosted playground README with Mermaid diagrams
javiercn cca0997
Add configurable apiPrefix/otlpPrefix, extract BlazorClientConfigurat…
javiercn 01fc91d
Remove MapConfigurationEndpoint from hosted playground
javiercn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...pireWithBlazorHosted/AspireWithBlazorHosted.AppHost/AspireWithBlazorHosted.AppHost.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <IsAspireHost>true</IsAspireHost> | ||
| <UserSecretsId>a1b2c3d4-e5f6-7890-abcd-ef1234567890</UserSecretsId> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <Compile Include="..\..\KnownResourceNames.cs" Link="KnownResourceNames.cs" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting" /> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting.AppHost" /> | ||
| <AspireProjectOrPackageReference Include="Aspire.Hosting.Blazor" /> | ||
| <!-- AppHost runs on .NET 10; pin Http to 10.x to match Aspire.Hosting --> | ||
| <PackageReference Include="Microsoft.Extensions.Http" VersionOverride="$(MicrosoftExtensionsHttpPreviewVersion)" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireWithBlazorHosted.Server\AspireWithBlazorHosted.csproj" /> | ||
| <ProjectReference Include="..\AspireWithBlazorHosted.WeatherApi\AspireWithBlazorHosted.WeatherApi.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
9 changes: 9 additions & 0 deletions
9
playground/AspireWithBlazorHosted/AspireWithBlazorHosted.AppHost/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| var builder = DistributedApplication.CreateBuilder(args); | ||
|
|
||
| var weatherApi = builder.AddProject<Projects.AspireWithBlazorHosted_WeatherApi>("weatherapi"); | ||
|
|
||
| builder.AddProject<Projects.AspireWithBlazorHosted>("blazorapp") | ||
| .ProxyService(weatherApi) | ||
| .ProxyTelemetry(); | ||
|
|
||
| builder.Build().Run(); |
17 changes: 17 additions & 0 deletions
17
...ound/AspireWithBlazorHosted/AspireWithBlazorHosted.AppHost/Properties/launchSettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "profiles": { | ||
| "https": { | ||
| "commandName": "Project", | ||
| "dotnetRunMessages": true, | ||
| "launchBrowser": true, | ||
| "applicationUrl": "https://localhost:17199", | ||
| "environmentVariables": { | ||
| "ASPNETCORE_ENVIRONMENT": "Development", | ||
| "DOTNET_ENVIRONMENT": "Development", | ||
| "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21199", | ||
| "DOTNET_DASHBOARD_OTLP_HTTP_ENDPOINT_URL": "https://localhost:18890", | ||
| "DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22199" | ||
| } | ||
| } | ||
| } | ||
| } |
21 changes: 21 additions & 0 deletions
21
...AspireWithBlazorHosted/AspireWithBlazorHosted.Client/AspireWithBlazorHosted.Client.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.BlazorWebAssembly"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| <NoDefaultLaunchSettingsFile>true</NoDefaultLaunchSettingsFile> | ||
| <StaticWebAssetProjectMode>Default</StaticWebAssetProjectMode> | ||
| <BlazorDisableThrowNavigationException>true</BlazorDisableThrowNavigationException> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" VersionOverride="8.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\AspireWithBlazorHosted.ClientServiceDefaults\AspireWithBlazorHosted.ClientServiceDefaults.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
19 changes: 19 additions & 0 deletions
19
playground/AspireWithBlazorHosted/AspireWithBlazorHosted.Client/Pages/Counter.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| @page "/counter" | ||
| @rendermode InteractiveWebAssembly | ||
|
|
||
| <PageTitle>Counter</PageTitle> | ||
|
|
||
| <h1>Counter</h1> | ||
|
|
||
| <p role="status">Current count: @currentCount</p> | ||
|
|
||
| <button class="btn btn-primary" @onclick="IncrementCount">Click me</button> | ||
|
|
||
| @code { | ||
| private int currentCount = 0; | ||
|
|
||
| private void IncrementCount() | ||
| { | ||
| currentCount++; | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
playground/AspireWithBlazorHosted/AspireWithBlazorHosted.Client/Pages/Weather.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| @page "/weather" | ||
| @rendermode InteractiveWebAssembly | ||
| @inject IHttpClientFactory HttpClientFactory | ||
|
|
||
| <PageTitle>Weather</PageTitle> | ||
|
|
||
| <h1>Weather</h1> | ||
|
|
||
| <p>This component demonstrates fetching data from the WeatherAPI service via Aspire service discovery.</p> | ||
|
|
||
| @if (errorMessage != null) | ||
| { | ||
| <div class="alert alert-danger">@errorMessage</div> | ||
| } | ||
| else if (forecasts == null) | ||
| { | ||
| <p><em>Loading...</em></p> | ||
| } | ||
| else | ||
| { | ||
| <table class="table"> | ||
| <thead> | ||
| <tr> | ||
| <th>Date</th> | ||
| <th>Temp. (C)</th> | ||
| <th>Temp. (F)</th> | ||
| <th>Summary</th> | ||
| </tr> | ||
| </thead> | ||
| <tbody> | ||
| @foreach (var forecast in forecasts) | ||
| { | ||
| <tr> | ||
| <td>@forecast.Date.ToShortDateString()</td> | ||
| <td>@forecast.TemperatureC</td> | ||
| <td>@forecast.TemperatureF</td> | ||
| <td>@forecast.Summary</td> | ||
| </tr> | ||
| } | ||
| </tbody> | ||
| </table> | ||
| } | ||
|
|
||
| @code { | ||
| private WeatherForecast[]? forecasts; | ||
| private string? errorMessage; | ||
|
|
||
| protected override async Task OnInitializedAsync() | ||
| { | ||
| try | ||
| { | ||
| var client = HttpClientFactory.CreateClient("weatherapi"); | ||
| forecasts = await client.GetFromJsonAsync<WeatherForecast[]>("weatherforecast"); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| errorMessage = $"Failed to fetch weather data: {ex.Message}"; | ||
| } | ||
| } | ||
|
|
||
| public class WeatherForecast | ||
| { | ||
| public DateOnly Date { get; set; } | ||
| public int TemperatureC { get; set; } | ||
| public string? Summary { get; set; } | ||
| public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
playground/AspireWithBlazorHosted/AspireWithBlazorHosted.Client/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| using Microsoft.AspNetCore.Components.WebAssembly.Hosting; | ||
| using Microsoft.Extensions.Hosting; | ||
| using OpenTelemetry.Metrics; | ||
| using OpenTelemetry.Trace; | ||
|
|
||
| var builder = WebAssemblyHostBuilder.CreateDefault(args); | ||
|
|
||
| // Environment variables are injected via JS initializer into MonoConfig.environmentVariables. | ||
| // They are available via Environment.GetEnvironmentVariable() but NOT automatically in IConfiguration. | ||
| // Service Discovery reads from IConfiguration, so we add environment variables to configuration. | ||
| builder.Configuration.AddEnvironmentVariables(); | ||
|
|
||
| // Add Aspire service defaults (OpenTelemetry, service discovery, resilience) | ||
| builder.AddBlazorClientServiceDefaults(); | ||
|
|
||
| // Named HttpClient for the weather API - uses service discovery to resolve "weatherapi" | ||
| builder.Services.AddHttpClient("weatherapi", client => | ||
| { | ||
| client.BaseAddress = new Uri("https+http://weatherapi"); | ||
| }); | ||
|
|
||
| var host = builder.Build(); | ||
|
|
||
| // WebAssembly does not support IHostedService, so TelemetryHostedService is never started. | ||
| // We must force initialization of MeterProvider and TracerProvider manually. | ||
| // See: https://github.com/dotnet/aspire/issues/2816 | ||
| _ = host.Services.GetService<MeterProvider>(); | ||
| _ = host.Services.GetService<TracerProvider>(); | ||
|
|
||
| await host.RunAsync(); |
9 changes: 9 additions & 0 deletions
9
playground/AspireWithBlazorHosted/AspireWithBlazorHosted.Client/_Imports.razor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| @using System.Net.Http | ||
| @using System.Net.Http.Json | ||
| @using Microsoft.AspNetCore.Components.Forms | ||
| @using Microsoft.AspNetCore.Components.Routing | ||
| @using Microsoft.AspNetCore.Components.Web | ||
| @using static Microsoft.AspNetCore.Components.Web.RenderMode | ||
| @using Microsoft.AspNetCore.Components.Web.Virtualization | ||
| @using Microsoft.JSInterop | ||
| @using AspireWithBlazorHosted.Client |
8 changes: 8 additions & 0 deletions
8
...AspireWithBlazorHosted/AspireWithBlazorHosted.Client/wwwroot/appsettings.Development.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
8 changes: 8 additions & 0 deletions
8
playground/AspireWithBlazorHosted/AspireWithBlazorHosted.Client/wwwroot/appsettings.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "Logging": { | ||
| "LogLevel": { | ||
| "Default": "Information", | ||
| "Microsoft.AspNetCore": "Warning" | ||
| } | ||
| } | ||
| } |
23 changes: 23 additions & 0 deletions
23
...ithBlazorHosted.ClientServiceDefaults/AspireWithBlazorHosted.ClientServiceDefaults.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk.Razor"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>$(DefaultTargetFramework)</TargetFramework> | ||
| <Nullable>enable</Nullable> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <SupportedPlatform Include="browser" /> | ||
| </ItemGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" /> | ||
| <PackageReference Include="Microsoft.Extensions.Http.Resilience" /> | ||
| <PackageReference Include="Microsoft.Extensions.ServiceDiscovery" /> | ||
| <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" /> | ||
| <PackageReference Include="OpenTelemetry.Extensions.Hosting" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.Http" /> | ||
| <PackageReference Include="OpenTelemetry.Instrumentation.Runtime" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is also a standalone app. Should that be added here? Or should its source code be deleted?