Skip to content

Blazor WebAssemblyHostBuilder initialization fails if root component's assembly is not loaded on client startup #26601

@tehaucave

Description

@tehaucave

Describe the bug

Blazor WebAssembly (5.0-rc1) client project startup fails when prerendering is used and the root component is defined in a razor class library, unless the client project's Main method explicitly references the razor class library assembly.

This appears to be the case because WebAssemblyHostBuilder.CreateDefault(...) only searches loaded assemblies for the root component type specified in the <component> tag in the server-side host page. It is not sufficient for the client project to reference the razor class library project. The client's Main method must explicitly cause the razor class library assembly to load before WebAssemblyHostBuilder.CreateDefault(...) is called.

I understand that most Blazor WebAssembly apps will define their root component in the client-side entry assembly, but it is sometimes convenient to define all of a Blazor app's UI components, including the root component, in a class library project that can be used in both Blazor Server and Blazor WebAssembly apps. While the client initialization error can be worked around, the workaround is not obvious and the error message (see below) is unhelpful.

To Reproduce

See repro project at https://github.com/tehaucave/RootComponentFromClassLibrary

This is a standard ASP.NET Core-hosted Blazor WebAssembly (5.0-rc1) solution with all components removed from the client project, and a razor class library project added containing a single razor component named AppRoot. Also, index.html has been removed from the client project and a _Host.cshtml page has been added to the server project and configured as the server's fallback page. A <component> tag is included in _Host.cshtml with type=AppRoot and render-mode=WebAssemblyPrerendered. In the client's Program.Main method, the explicit root component registration has been removed.

The client app throws a runtime exception on startup unless I add code like the following before the call to WebAssemblyHostBuilder.CreateDefault(args):

typeof(AppRoot).ToString()

Exceptions (if any)

From browser console (chrome):

blazor.webassembly.js:1 System.ArgumentNullException: Value cannot be null. (Parameter 'componentType')
at Microsoft.AspNetCore.Components.WebAssembly.Hosting.RootComponentMappingCollection.Add(Type componentType, String selector, ParameterView parameters)
at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.InitializeRegisteredRootComponents(WebAssemblyJSRuntimeInvoker jsRuntimeInvoker)
at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder..ctor(WebAssemblyJSRuntimeInvoker jsRuntimeInvoker)
at Microsoft.AspNetCore.Components.WebAssembly.Hosting.WebAssemblyHostBuilder.CreateDefault(String[] args)
at RootComponentFromClassLibrary.Client.Program.Main(String[] args) in C:\Users\a060514\source\repos\github\RootComponentFromClassLibrary\Client\Program.cs:line 25
at Microsoft.AspNetCore.Components.WebAssembly.Hosting.EntrypointInvoker.InvokeEntrypoint(String assemblyName, String[] args)

Further technical details

  • ASP.NET Core version 5.0-rc1

  • Visual Studio 2019 16.8.0 Preview 3.2

  • Include the output of dotnet --info

.NET SDK (reflecting any global.json):
Version: 5.0.100-rc.1.20452.10
Commit: 473d1b592e

Runtime Environment:
OS Name: Windows
OS Version: 10.0.18362
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\5.0.100-rc.1.20452.10\

Host (useful for support):
Version: 5.0.0-rc.1.20451.14
Commit: 38017c3935

.NET SDKs installed:
3.1.100 [C:\Program Files\dotnet\sdk]
3.1.300-preview-015048 [C:\Program Files\dotnet\sdk]
3.1.300 [C:\Program Files\dotnet\sdk]
3.1.400-preview-015178 [C:\Program Files\dotnet\sdk]
3.1.401 [C:\Program Files\dotnet\sdk]
5.0.100-preview.8.20417.9 [C:\Program Files\dotnet\sdk]
5.0.100-rc.1.20452.10 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
Microsoft.AspNetCore.All 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0-preview.8.20414.8 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 5.0.0-rc.1.20451.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0-preview.8.20407.11 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 5.0.0-rc.1.20451.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.1.4 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 3.1.7 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0-preview.8.20411.6 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
Microsoft.WindowsDesktop.App 5.0.0-rc.1.20452.2 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

To install additional .NET runtimes or SDKs:
https://aka.ms/dotnet-download

  • The IDE (VS / VS Code/ VS4Mac) you're running on, and it's version

Metadata

Metadata

Assignees

No one assigned

    Labels

    Blazor ♥ MVCIssues related to integration between Blazor and MVC/Razor pagesPillar: Technical Debtaffected-very-fewThis issue impacts very few customersarea-blazorIncludes: Blazor, Razor ComponentsbugThis issue describes a behavior which is not expected - a bug.feature-blazor-boot-upfeature-blazor-wasmThis issue is related to and / or impacts Blazor WebAssemblyfeature-prerenderingIssues related to prerendering blazor componentsseverity-blockingThis label is used by an internal tool

    Type

    No type

    Projects

    Status

    Done

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions