Default Services

Among others:

IJSRuntimeSingletonAn instance of a JavaScript runtime where JS calls are dispatched
NavigationManagerSingletonHelpers for working with URI and navigation state

Adding & Using Services

Note: Blazor WASM apps do not have Scoped DI services. If registered as Scoped, they will behave like Singleton. This results in services having a longer lifetime than typical ASP.NET Core apps.


var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.Services.AddSingleton<ISomeService, SomeService>();

var host = builder.Build();

// After the host is built, but before it's run, services are available from the root DI scope:
var weatherService = host.Services.GetRequiredService<WeatherService>();

// The host can also access configuration:
await weatherService.InitializeWeatherAsync(host.Configuration["WeatherServiceUri"]);

await host.RunAsync();

Adding & Using Services — Hosted Blazor WASM

If common services are required between the Server and Client projects in a hosted Blazor WASM solution, place common service registrations in the Client project and call the method to register the services in both:

// Factor common service registrations into a separate method:
public static void ConfigureCommonServices(IServiceCollection services)

In Client’s Program.cs, register the common services:

var builder = WebAssemblyHostBuilder.CreateDefault(args);

In Server’s Program.cs, do the same, calling the Client’s ConfigureCommonServices method:

var builder = WebApplication.CreateBuilder(args);

Detecting Transient Disposables in Blazor WASM Apps