Aspnetcore.docs: Instructions for lazy loading with server side prerender incorrect

Created on 27 Sep 2020  Â·  2Comments  Â·  Source: dotnet/AspNetCore.Docs

It is said that you need to add this line into ConfigureServices in the Server project when using Server Prerender:
services.AddSingleton<LazyAssemblyLoader>();

This throws an error, because in a Server project, IJsRuntime has a scoped lifetime and the LazyAssemblyLoader tries to dependency inject it throws this error:
AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader Lifetime: Singleton ImplementationType: Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader': Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader'.)


Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Blazor P0 Source - Docs.ms doc-bug

Most helpful comment

@guardrex Argh! Yeah, this should be AddScoped. I think you might've actually had it set to that at first before I nudged you to the wrong solution.

All 2 comments

@captainsafia ... The guidance for the service registration is in the following section ...

https://docs.microsoft.com/aspnet/core/blazor/webassembly-lazy-load-assemblies?view=aspnetcore-5.0#assembly-load-logic-in-onnavigateasync

The framework's lazy loading implementation supports lazy loading with prerendering in a hosted Blazor solution. During prerendering, all assemblies, including those marked for lazy loading, are assumed to be loaded. Manually register LazyAssemblyLoader in the Server project's Startup.ConfigureServices method (Startup.cs):

services.AddSingleton<LazyAssemblyLoader>();

@guardrex Argh! Yeah, this should be AddScoped. I think you might've actually had it set to that at first before I nudged you to the wrong solution.

Was this page helpful?
0 / 5 - 0 ratings