Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Sep 25, 2025

Fixes #33537

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

Problem

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.

Solution

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
  • Minimal Footprint: Only ~40 lines of new code with no breaking changes

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
  • 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
  • Performance optimized - only processes hosted services if any are registered

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 #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

private readonly IEnumerable<IHostedService> _services;
and
private HostedServiceExecutor? _hostedServiceExecutor;

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 #63637

💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

Co-authored-by: javiercn <6995051+javiercn@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Support hosted services in WebAssemblyHost Support hosted services in WebAssemblyHost Sep 25, 2025
Copilot finished work on behalf of javiercn September 25, 2025 10:12
@Copilot Copilot AI requested a review from javiercn September 25, 2025 10:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support hosted services in WebAssemblyHost Add hosted service feature for WebAssemblyHost
2 participants