Skip to content

[CORRUPTED] Synthetic Benchmark PR #63814 - Support hosted services in WebAssemblyHost#30

Open
bar-qodo wants to merge 6 commits intobase_pr_63814_20260113_2357from
corrupted_pr_63814_20260113_2357
Open

[CORRUPTED] Synthetic Benchmark PR #63814 - Support hosted services in WebAssemblyHost#30
bar-qodo wants to merge 6 commits intobase_pr_63814_20260113_2357from
corrupted_pr_63814_20260113_2357

Conversation

@bar-qodo
Copy link

Benchmark PR dotnet#63814

Type: Corrupted (contains bugs)

Original PR Title: Support hosted services in WebAssemblyHost
Original PR Description: This PR adds support for hosted services (IHostedService) in WebAssemblyHost, enabling automatic startup and shutdown of background services in Blazor WebAssembly applications.

Description

Applications using WebAssemblyHost (e.g. Blazor WebAssembly clients) did not support hosted services out of the box. Services registered with AddHostedService were ignored when using WebAssemblyHostBuilder, unlike in applications built with WebApplicationBuilder or HostApplicationBuilder. This prevented libraries like OpenTelemetry from working properly in Blazor WebAssembly apps, as they rely on hosted services for initialization.

The implementation adds hosted service lifecycle management to WebAssemblyHost:

  • Service Discovery: Automatically discovers IHostedService implementations from the DI container
  • Startup Integration: Starts hosted services during RunAsyncCore() after component state restoration
  • Cleanup Integration: Stops hosted services during DisposeAsync() with proper error handling
  • HostedServiceExecutor: Encapsulates hosted service lifecycle logic in a dedicated executor class registered in DI, with integrated exception handling and logging using LoggerMessage source generator pattern
  • Clean Architecture: WebAssemblyHost simply resolves the executor from DI and calls it at the appropriate times, maintaining clean separation of concerns

Usage

Developers can now register hosted services in Blazor WebAssembly apps:

var builder = WebAssemblyHostBuilder.CreateDefault(args);

// Register hosted services - they'll start automatically
builder.Services.AddSingleton<IHostedService, MyBackgroundService>();

var host = builder.Build();
await host.RunAsync(); // Hosted services start here

Implementation Details

  • Added Microsoft.Extensions.Hosting.Abstractions reference to enable IHostedService support
  • Created HostedServiceExecutor class in the WebAssembly project to encapsulate hosted service lifecycle management
  • Registered HostedServiceExecutor as a singleton in DI in WebAssemblyHostBuilder.InitializeDefaultServices()
  • HostedServiceExecutor accepts ILogger<HostedServiceExecutor> and handles all exception aggregation and logging internally
  • Hosted services start after component state restoration (matching the timing suggested in the original issue)
  • Error handling ensures host disposal doesn't fail if hosted services throw exceptions during shutdown
  • Uses LoggerMessage source generator pattern for structured logging, following codebase conventions in WebAssemblyRenderer and WebAssemblyNavigationManager
  • WebAssemblyHost resolves the executor from DI and coordinates its invocation at the right times
  • Follows the same architectural pattern as the Hosting project for consistency across the codebase

Testing

Added comprehensive test coverage:

  • Service startup during host run
  • Service shutdown during host disposal
  • Error handling for faulty services
  • Integration with service registration patterns

All existing WebAssemblyHost functionality remains unchanged and all tests pass.

Original prompt

This section details on the original issue you should resolve

<issue_title>Support hosted services in WebAssemblyHost</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Applications using WebAssemblyHost (e.g. Blazor WebAssembly clients) do not support hosted services out of the box. Services registered with AddHostedService are ignored when using WebAssemblyHostBuilder, unlike in applications built with WebApplicationBuilder or HostApplicationBuilder.

Describe the solution you'd like

WebAssemblyHost should support automatically running hosted services registered with AddHostedService.

Additional context

This feature request has already been rejected in dotnet#33537. However, we might want to reconsider the decision. For example, support for hosted services might be beneficial for Open Telemetry integration in Blazor Wasm apps.</issue_description>

<agent_instructions>Look at https://github.com/dotnet/aspnetcore/blob/063ba34bff5a3b6ee2f54cafe83b04bd3d4a5c06/src/Hosting/Hosting/src/Internal/HostedServiceExecutor.cs#L10 and
https://github.com/dotnet/aspnetcore/blob/063ba34bff5a3b6ee2f54cafe83b04bd3d4a5c06/src/Hosting/Hosting/src/Internal/WebHost.cs#L30

We want WebAssemblyHost to start the services here https://github.com/dotnet/aspnetcore/blob/main/src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHost.cs#L139</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@pavelsavara We should measure the size/perf impact of adding it by default for WASM
Fixes dotnet#63637

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.
Original PR URL: dotnet#63814

Copilot AI and others added 6 commits September 25, 2025 09:53
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
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