Skip to content

Try register LazyAssemblyLoader to DI in AddInteractiveWebAssemblyCom… #61719

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

Closed

Conversation

oroztocil
Copy link
Member

Changes

  • Adds DI registration for LazyAssemblyLoader in AddInteractiveWebAssemblyComponents.
  • The type is loaded via reflection and only registered if it can be found.

Description

We want to register LazyAssemblyLoader in AddInteractiveWebAssemblyComponents in order to prevent the confusing situation where a component that injects LazyAssemblyLoader works in the WebAssembly client but crashes during prerendering. (Note that it works when the wasm client takes over because WebAssemblyHostBuilder.CreateDefault registers LazyAssemblyLoader.)

However, this could not be done as is without adding a problematic reference between projects. To avoid this, the implementation uses reflection to try to load the Microsoft.AspNetCore.Components.WebAssembly assembly and get the type. If the assembly or the type cannot be found, no error is thrown.

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label Apr 28, 2025
@oroztocil
Copy link
Member Author

This is an alternative solution to #61703. Only one of these should be merged.

@pavelsavara
Copy link
Member

please consider backport

@oroztocil oroztocil marked this pull request as ready for review May 7, 2025 08:50
@oroztocil oroztocil requested a review from a team as a code owner May 7, 2025 08:50
@@ -25,6 +30,15 @@ public static IRazorComponentsBuilder AddInteractiveWebAssemblyComponents(this I

builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<RenderModeEndpointProvider, WebAssemblyEndpointProvider>());

// Try register LazyAssemblyLoader to prevent crashes during prerendering.
// TODO: Remove this once LazyAssemblyLoader is no longer used.
Copy link
Member

Choose a reason for hiding this comment

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

remove the TODO please


private static void AssertServerProjectCanUseLazyAssemblyLoader(Project project)
{
var assemblyName = "Microsoft.AspNetCore.Components.WebAssembly.dll";
Copy link
Member

Choose a reason for hiding this comment

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

also check that the class is in the dll

// This assert is here to check that the assembly containing LazyAssemblyLoader is actually present in a standard app
// created from the Blazor Web template.
// See https://github.com/dotnet/aspnetcore/issues/51966.
// TODO: Remove this when LazyAssemblyLoader is no longer being used, or the dependency graph changes so reflection is no longer needed.
Copy link
Member

Choose a reason for hiding this comment

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

remove todo

@oroztocil
Copy link
Member Author

It turned out the much simpler fix was actually possible (#61822). I erroneously thought it required a project configuration change that we did not want to do (e.g. adding the assembly into the Shared Framework).

@oroztocil oroztocil closed this May 7, 2025
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview5 milestone May 7, 2025
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.

2 participants