Skip to content

Conversation

rolandVi
Copy link
Contributor

@rolandVi rolandVi commented Jul 9, 2025

Add UseDefaultServiceProvider extension methods for WebAssembly host builder

Summary

This pull request enhances the WebAssemblyHostBuilder by introducing support for customizable service provider options.

Problem Solved

Previously, circular DI dependencies in Blazor WebAssembly apps would cause the browser to hang with no error message. Now developers get validation errors at build time instead of runtime hangs, especially in development environments.

Changes Made

  • Added WebAssemblyHostBuilderExtensions class with two UseDefaultServiceProvider overloads
  • Added UseServiceProviderOptions method to WebAssemblyHostBuilder
  • Modified constructor to enable ValidateOnBuild = true by default in development environment
  • Added tests for the new functionality

Public API updates:

  • Updated PublicAPI.Unshipped.txt to include the new UseServiceProviderOptions method and UseDefaultServiceProvider extension methods

Test improvements:

  • Added tests to verify the functionality of UseDefaultServiceProvider
  • Added tests to ensure correct behavior with circular dependencies

API Usage

// Simple configuration
builder.UseDefaultServiceProvider(options => 
{
    options.ValidateOnBuild = true;
});

// Environment-aware configuration  
builder.UseDefaultServiceProvider((env, options) =>
{
    options.ValidateOnBuild = env.IsDevelopment();
});

Fixes #43866

@rolandVi rolandVi requested a review from a team as a code owner July 9, 2025 11:39
@github-actions github-actions bot added the area-blazor Includes: Blazor, Razor Components label Jul 9, 2025
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Jul 9, 2025
Copy link
Member

@javiercn javiercn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

@ilonatommy ilonatommy removed the community-contribution Indicates that the PR has been added by a community member label Jul 10, 2025
@ilonatommy ilonatommy added this to the 10.0-preview7 milestone Jul 10, 2025
@ilonatommy ilonatommy merged commit f469cdc into dotnet:main Jul 10, 2025
29 checks passed
@rolandVi rolandVi deleted the rolandVi/blazor-wasm-circular-di-diagnostics branch August 14, 2025 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better diagnostics/information for circular DI dependencies in Blazor WebAssembly
3 participants