Skip to content
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

docs(common): Revamp WASM lazy loading KB #1829

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 73 additions & 5 deletions knowledge-base/common-lazy-load-assemblies-wasm.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,85 @@ description: How to use the lazy assembly loading feature of Blazor with the Tel
type: how-to
page_title: Telerik Blazor Components and WebAssembly Lazy Load of Assemblies
slug: common-kb-lazy-load-assemblies-wasm
position:
tags:
position:
tags:
ticketid: 1628239, 1633572
res_type: kb
---

## Environment

## Description
<table>
<tbody>
<tr>
<td>UI for Blazor version</td>
<td>2.19.0 and above</td>
</tr>
<tr>
<td>.NET version</td>
<td>5 and above</td>
</tr>
</tbody>
</table>

How to use the lazy assembly loading feature of Blazor with the Telerik components?
## Description

How to use the [lazy assembly loading feature of Blazor WebAssembly apps](https://learn.microsoft.com/en-us/aspnet/core/blazor/webassembly-lazy-load-assemblies) with the Telerik Blazor components?

## Solution

An example is available in the following project: [https://github.com/telerik/blazor-ui/tree/master/common/lazy-load-assemblies-wasm](https://github.com/telerik/blazor-ui/tree/master/common/lazy-load-assemblies-wasm).
All general guidance from the [Microsoft documentation](https://learn.microsoft.com/en-us/aspnet/core/blazor/webassembly-lazy-load-assemblies) applies. The Telerik-related specifics are:

* List the following assemblies in the "client" `.csproj` file to be lazy loaded.

````
<ItemGroup>
<!-- Components and data binding -->
<BlazorWebAssemblyLazyLoad Include="Telerik.Blazor.dll" />
<BlazorWebAssemblyLazyLoad Include="Telerik.DataSource.dll" />
<BlazorWebAssemblyLazyLoad Include="System.Data.Common.dll" />
<BlazorWebAssemblyLazyLoad Include="System.Linq.Queryable.dll" />
<!-- Icons -->
<BlazorWebAssemblyLazyLoad Include="Telerik.SvgIcons.dll" />
<BlazorWebAssemblyLazyLoad Include="Telerik.FontIcons.dll" />
<!-- PivotGrid -->
<BlazorWebAssemblyLazyLoad Include="Telerik.Pivot.Core.dll" />
<BlazorWebAssemblyLazyLoad Include="Telerik.Pivot.DataProviders.Xmla.dll" />
<!-- Scheduler -->
<BlazorWebAssemblyLazyLoad Include="Telerik.Recurrence.dll" />
<!-- Excel export -->
<BlazorWebAssemblyLazyLoad Include="Telerik.Documents.SpreadsheetStreaming.dll" />
<BlazorWebAssemblyLazyLoad Include="Telerik.Zip.dll" />
</ItemGroup>
````

* The assembly requirements depend on component usage, and not on feature usage. For example, both icon assemblies are always required, as our components render icons internally and must be aware of both types of icons. The assemblies, which are related to Excel export, are always required when using a Grid. `Telerik.Recurrence.dll` is required only when using the Scheduler.
* Move the `<TelerikRootComponent>` to a layout that is used only on pages that have the Telerik assemblies loaded.
* Lazy loading of assemblies does not support dynamic service injection. As a result, remove the Telerik service registration (`builder.Services.AddTelerikBlazor();`) from `Program.cs`. If you are using [localization for the Telerik Blazor components]({%slug globalization-localization%}), define the the localization service for the Telerik components with the `Localizer` parameter of the `<TelerikRootComponent>`. The key thing is to instantiate the localization service inline. It cannot be injected as a variable from the `@code { }` block, because that will throw runtime errors.

````
@using LazyLoadTelerikComponents.Shared.Services

<TelerikRootComponent Localizer="@( new SampleResxLocalizer() )">
...
</TelerikRootComponent>
````

Overall, the lazy loading of assemblies at the correct time is a responsibility of the application. If an assembly is not loaded when required, the app will throw `System.IO.FileNotFoundException: Could not load file or assembly ...`. The loading code is in the `OnNavigateAsync` event handler of the `<Router>`. You can also define an optional loading screen inside the `<Router>` with a `<Navigating>` tag.

### .NET 8 Specifics

The following tips apply to .NET 8 WebAssembly apps only:

* Use `.wasm` instead of `.dll` in the `.csproj` file and the `OnNavigateAsync` event handler.
* [Register the lazy loader service manually](https://github.com/dotnet/aspnetcore/issues/51966) in the "server" `Program.cs`. Otherwise, you may get a `InvalidOperationException: Cannot provide a value for property 'AssemblyLoader' on type '...Routes'. There is no registered service of type 'Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader'.`

````
using Microsoft.AspNetCore.Components.WebAssembly.Services;

builder.Services.AddScoped(typeof(LazyAssemblyLoader));
````

## Example

An sample project is available in GitHub: [Lazy load Telerik Blazor assemblies](https://github.com/telerik/blazor-ui/tree/master/common/lazy-load-assemblies-wasm).
Loading