--- title: Lazy load assemblies in ASP.NET Core Blazor WebAssembly author: guardrex description: Discover how to lazy load assemblies in Blazor WebAssembly apps. monikerRange: '>= aspnetcore-5.0' ms.author: riande ms.custom: mvc ms.date: 11/08/2022 uid: blazor/webassembly-lazy-load-assemblies --- # Lazy load assemblies in ASP.NET Core Blazor WebAssembly [!INCLUDE[](~/includes/not-latest-version.md)] Blazor WebAssembly app startup performance can be improved by waiting to load app assemblies until the assemblies are required, which is called *lazy loading*. This article's initial sections cover the app configuration. For a working demonstration, see the [Complete example](#complete-example) section at the end of this article. *This article only applies to Blazor WebAssembly apps.* Assembly lazy loading doesn't benefit Blazor Server apps because Blazor Server app assemblies aren't downloaded to the client. ## Project file configuration Mark assemblies for lazy loading in the app's project file (`.csproj`) using the `BlazorWebAssemblyLazyLoad` item. Use the assembly name with the `.dll` extension. The Blazor framework prevents the assembly from loading at app launch. ```xml ``` The `{ASSEMBLY NAME}` placeholder is the name of the assembly. The `.dll` file extension is required. Include one `BlazorWebAssemblyLazyLoad` item for each assembly. If an assembly has dependencies, include a `BlazorWebAssemblyLazyLoad` entry for each dependency. ## `Router` component configuration The Blazor framework automatically registers a singleton service for lazy loading assemblies in client-side Blazor WebAssembly apps†, . The method: * Uses [JS interop](xref:blazor/js-interop/call-dotnet-from-javascript) to fetch assemblies via a network call. * Loads assemblies into the runtime executing on WebAssembly in the browser. †Guidance for *hosted* Blazor WebAssembly [solutions](xref:blazor/tooling#visual-studio-solution-file-sln) is covered in the [Lazy load assemblies in a hosted Blazor WebAssembly solution](#lazy-load-assemblies-in-a-hosted-blazor-webassembly-solution) section. Blazor's component designates the assemblies that Blazor searches for routable components and is also responsible for rendering the component for the route where the user navigates. The component's [`OnNavigateAsync` method](xref:blazor/fundamentals/routing#handle-asynchronous-navigation-events-with-onnavigateasync) is used in conjunction with lazy loading to load the correct assemblies for endpoints that a user requests. Logic is implemented inside to determine the assemblies to load with . Options for how to structure the logic include: * Conditional checks inside the method. * A lookup table that maps routes to assembly names, either injected into the component or implemented within the [`@code`](xref:mvc/views/razor#code) block. In the following example: * The namespace for is specified. * The service is injected (`AssemblyLoader`). * The `{PATH}` placeholder is the path where the list of assemblies should load. The example uses a conditional check for a single path that loads a single set of assemblies. * The `{LIST OF ASSEMBLIES}` placeholder is the comma-separated list of assembly file name strings, including their `.dll` extensions (for example, `"Assembly1.dll", "Assembly2.dll"`). `App.razor`: :::moniker range=">= aspnetcore-6.0" ```razor @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.WebAssembly.Services @using Microsoft.Extensions.Logging @inject LazyAssemblyLoader AssemblyLoader @inject ILogger Logger ... @code { private async Task OnNavigateAsync(NavigationContext args) { try { if (args.Path == "{PATH}") { var assemblies = await AssemblyLoader.LoadAssembliesAsync( new[] { {LIST OF ASSEMBLIES} }); } } catch (Exception ex) { Logger.LogError("Error: {Message}", ex.Message); } } } ``` :::moniker-end :::moniker range="< aspnetcore-6.0" ```razor @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.WebAssembly.Services @using Microsoft.Extensions.Logging @inject LazyAssemblyLoader AssemblyLoader @inject ILogger Logger ... @code { private async Task OnNavigateAsync(NavigationContext args) { try { if (args.Path == "{PATH}") { var assemblies = await AssemblyLoader.LoadAssembliesAsync( new[] { {LIST OF ASSEMBLIES} }); } } catch (Exception ex) { Logger.LogError("Error: {Message}", ex.Message); } } } ``` :::moniker-end > [!NOTE] > The preceding example doesn't show the contents of the `Router` component's Razor markup (`...`). For a demonstration with complete code, see the [Complete example](#complete-example) section of this article. :::moniker range="= aspnetcore-5.0" [!INCLUDE[](~/blazor/includes/prefer-exact-matches.md)] :::moniker-end ## Assemblies that include routable components When the list of assemblies includes routable components, the assembly list for a given path is passed to the `Router` component's collection. In the following example: * The [List](xref:System.Collections.Generic.List%601)\<> in `lazyLoadedAssemblies` passes the assembly list to . The framework searches the assemblies for routes and updates the route collection if new routes are found. To access the type, the namespace for is included at the top of the `App.razor` file. * The `{PATH}` placeholder is the path where the list of assemblies should load. The example uses a conditional check for a single path that loads a single set of assemblies. * The `{LIST OF ASSEMBLIES}` placeholder is the comma-separated list of assembly file name strings, including their `.dll` extensions (for example, `"Assembly1.dll", "Assembly2.dll"`). `App.razor`: :::moniker range=">= aspnetcore-6.0" ```razor @using System.Reflection @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.WebAssembly.Services @using Microsoft.Extensions.Logging @inject LazyAssemblyLoader AssemblyLoader @inject ILogger Logger ... @code { private List lazyLoadedAssemblies = new(); private async Task OnNavigateAsync(NavigationContext args) { try { if (args.Path == "{PATH}") { var assemblies = await AssemblyLoader.LoadAssembliesAsync( new[] { {LIST OF ASSEMBLIES} }); lazyLoadedAssemblies.AddRange(assemblies); } } catch (Exception ex) { Logger.LogError("Error: {Message}", ex.Message); } } } ``` :::moniker-end :::moniker range="< aspnetcore-6.0" ```razor @using System.Reflection @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.WebAssembly.Services @using Microsoft.Extensions.Logging @inject LazyAssemblyLoader AssemblyLoader @inject ILogger Logger ... @code { private List lazyLoadedAssemblies = new List(); private async Task OnNavigateAsync(NavigationContext args) { try { if (args.Path == "{PATH}") { var assemblies = await AssemblyLoader.LoadAssembliesAsync( new[] { {LIST OF ASSEMBLIES} }); lazyLoadedAssemblies.AddRange(assemblies); } } catch (Exception ex) { Logger.LogError("Error: {Message}", ex.Message); } } } ``` :::moniker-end > [!NOTE] > The preceding example doesn't show the contents of the `Router` component's Razor markup (`...`). For a demonstration with complete code, see the [Complete example](#complete-example) section of this article. :::moniker range="= aspnetcore-5.0" [!INCLUDE[](~/blazor/includes/prefer-exact-matches.md)] :::moniker-end For more information, see . ## User interaction with `` content While loading assemblies, which can take several seconds, the component can indicate to the user that a page transition is occurring with the router's property. For more information, see . ## Handle cancellations in `OnNavigateAsync` The object passed to the callback contains a that's set when a new navigation event occurs. The callback must throw when the cancellation token is set to avoid continuing to run the callback on an outdated navigation. For more information, see . ## `OnNavigateAsync` events and renamed assembly files The resource loader relies on the assembly names that are defined in the `blazor.boot.json` file. If [assemblies are renamed](xref:blazor/host-and-deploy/webassembly#change-the-file-name-extension-of-dll-files), the assembly names used in an callback and the assembly names in the `blazor.boot.json` file are out of sync. To rectify this: * Check to see if the app is running in the `Production` environment when determining which assembly names to use. * Store the renamed assembly names in a separate file and read from that file to determine what assembly name to use with the service and callback. ## Lazy load assemblies in a hosted Blazor WebAssembly solution The framework's lazy loading implementation supports lazy loading with prerendering in a hosted Blazor WebAssembly [solution](xref:blazor/tooling#visual-studio-solution-file-sln). During prerendering, all assemblies, including those marked for lazy loading, are assumed to be loaded. Manually register the service in the **:::no-loc text="Server":::** project. :::moniker range=">= aspnetcore-6.0" At the top of the `Program.cs` file of the **:::no-loc text="Server":::** project, add the namespace for : ```csharp using Microsoft.AspNetCore.Components.WebAssembly.Services; ``` In `Program.cs` of the **:::no-loc text="Server":::** project, register the service: ```csharp builder.Services.AddScoped(); ``` :::moniker-end :::moniker range="< aspnetcore-6.0" At the top of the `Startup.cs` file of the **:::no-loc text="Server":::** project, add the namespace for : ```csharp using Microsoft.AspNetCore.Components.WebAssembly.Services; ``` In `Startup.ConfigureServices` (`Startup.cs`) of the **:::no-loc text="Server":::** project, register the service: ```csharp services.AddScoped(); ``` :::moniker-end ## Complete example The demonstration in this section: * Creates a robot controls assembly (`GrantImaharaRobotControls.dll`) as a [Razor class library (RCL)](xref:blazor/components/class-libraries) that includes a `Robot` component (`Robot.razor` with a route template of `/robot`). * Lazily loads the RCL's assembly to render its `Robot` component when the `/robot` URL is requested by the user. 1. Create a new ASP.NET Core class library project: * Visual Studio: **Create a solution** > **Create a new project** > **Razor Class Library**. Name the project `GrantImaharaRobotControls`. * Visual Studio Code/.NET CLI: Execute `dotnet new razorclasslib -o GrantImaharaRobotControls` from a command prompt. The `-o|--output` option creates a folder for the [solution](xref:blazor/tooling#visual-studio-solution-file-sln) and names the project `GrantImaharaRobotControls`. 1. The example component presented later in this section uses a [Blazor form](xref:blazor/forms-and-input-components). In the RCL project, add the [`Microsoft.AspNetCore.Components.Forms`](https://www.nuget.org/packages/Microsoft.AspNetCore.Components.Forms) package to the project. [!INCLUDE[](~/includes/package-reference.md)] 1. Create a `HandGesture` class in the RCL with a `ThumbUp` method that hypothetically makes a robot perform a thumbs-up gesture. The method accepts an argument for the axis, `Left` or `Right`, as an [`enum`](/dotnet/csharp/language-reference/builtin-types/enum). The method returns `true` on success. `HandGesture.cs`: :::moniker range=">= aspnetcore-6.0" ```csharp using Microsoft.Extensions.Logging; namespace GrantImaharaRobotControls; public static class HandGesture { public static bool ThumbUp(Axis axis, ILogger logger) { logger.LogInformation("Thumb up gesture. Axis: {Axis}", axis); // Code to make robot perform gesture return true; } } public enum Axis { Left, Right } ``` :::moniker-end :::moniker range="< aspnetcore-6.0" ```csharp using Microsoft.Extensions.Logging; namespace GrantImaharaRobotControls { public static class HandGesture { public static bool ThumbUp(Axis axis, ILogger logger) { logger.LogInformation("Thumb up gesture. Axis: {Axis}", axis); // Code to make robot perform gesture return true; } } public enum Axis { Left, Right } } ``` :::moniker-end 1. Add the following component to the root of the RCL project. The component permits the user to submit a left or right hand thumb-up gesture request. `Robot.razor`: :::moniker range=">= aspnetcore-6.0" ```razor @page "/robot" @using Microsoft.AspNetCore.Components.Forms @using Microsoft.Extensions.Logging @inject ILogger Logger

Robot

@foreach (var entry in (Axis[])Enum .GetValues(typeof(Axis))) {  @entry
}

@message

@code { private RobotModel robotModel = new() { AxisSelection = Axis.Left }; private string? message; private void HandleValidSubmit() { Logger.LogInformation("HandleValidSubmit called"); var result = HandGesture.ThumbUp(robotModel.AxisSelection, Logger); message = $"ThumbUp returned {result} at {DateTime.Now}."; } public class RobotModel { public Axis AxisSelection { get; set; } } } ``` :::moniker-end :::moniker range="< aspnetcore-6.0" ```razor @page "/robot" @using Microsoft.AspNetCore.Components.Forms @using Microsoft.Extensions.Logging @inject ILogger Logger

Robot

@foreach (var entry in (Axis[])Enum .GetValues(typeof(Axis))) {  @entry
}

@message

@code { private RobotModel robotModel = new RobotModel() { AxisSelection = Axis.Left }; private string message; private void HandleValidSubmit() { Logger.LogInformation("HandleValidSubmit called"); var result = HandGesture.ThumbUp(robotModel.AxisSelection, Logger); message = $"ThumbUp returned {result} at {DateTime.Now}."; } public class RobotModel { public Axis AxisSelection { get; set; } } } ``` :::moniker-end Create a Blazor WebAssembly app to demonstrate lazy loading of the RCL's assembly: 1. Create the Blazor WebAssembly app in Visual Studio, Visual Studio Code, or via a command prompt with the .NET CLI. Name the project `LazyLoadTest`. 1. Create a project reference for the `GrantImaharaRobotControls` RCL: * Visual Studio: Add the `GrantImaharaRobotControls` RCL project to the solution (**Add** > **Existing Project**). Select **Add** > **Project Reference** to add a project reference for the `GrantImaharaRobotControls` RCL. * Visual Studio Code/.NET CLI: Execute `dotnet add reference {PATH}` in a command shell from the project's folder. The `{PATH}` placeholder is the path to the RCL project. Build and run the app. For the default page that loads the `Index` component (`Pages/Index.razor`), the developer tool's Network tab indicates that the RCL's assembly `GrantImaharaRobotControls.dll` is loaded. The `Index` component makes no use of the assembly, so loading the assembly is inefficient. ![Index component loaded in the browser with developer tool's Network tab indicating that the GrantImaharaRobotControls.dll assembly is loaded.](~/blazor/webassembly-lazy-load-assemblies/_static/screenshot1.png) Configure the app to lazy load the `GrantImaharaRobotControls.dll` assembly: 1. Specify the RCL's assembly for lazy loading in the Blazor WebAssembly app's project file (`.csproj`): ```xml ``` 1. The following component demonstrates loading the `GrantImaharaRobotControls.dll` assembly when the user navigates to `/robot`. Replace the app's default `App` component with the following `App` component. During page transitions, a styled message is displayed to the user with the `` element. For more information, see the [User interaction with `` content](#user-interaction-with-navigating-content) section. The assembly is assigned to , which results in the router searching the assembly for routable components, where it finds the `Robot` component. The `Robot` component's route is added to the app's route collection. For more information, see the article and the [Assemblies that include routable components](#assemblies-that-include-routable-components) section of this article. `App.razor`: :::moniker range=">= aspnetcore-6.0" ```razor @using System.Reflection @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.WebAssembly.Services @using Microsoft.Extensions.Logging @inject LazyAssemblyLoader AssemblyLoader @inject ILogger Logger

Loading the requested page…

Sorry, there's nothing at this address.

@code { private List lazyLoadedAssemblies = new(); private async Task OnNavigateAsync(NavigationContext args) { try { if (args.Path == "robot") { var assemblies = await AssemblyLoader.LoadAssembliesAsync( new[] { "GrantImaharaRobotControls.dll" }); lazyLoadedAssemblies.AddRange(assemblies); } } catch (Exception ex) { Logger.LogError("Error: {Message}", ex.Message); } } } ``` :::moniker-end :::moniker range="< aspnetcore-6.0" ```razor @using System.Reflection @using Microsoft.AspNetCore.Components.Routing @using Microsoft.AspNetCore.Components.WebAssembly.Services @using Microsoft.Extensions.Logging @inject LazyAssemblyLoader AssemblyLoader @inject ILogger Logger

Loading the requested page…

Sorry, there's nothing at this address.

@code { private List lazyLoadedAssemblies = new List(); private async Task OnNavigateAsync(NavigationContext args) { try { if (args.Path == "robot") { var assemblies = await AssemblyLoader.LoadAssembliesAsync( new[] { "GrantImaharaRobotControls.dll" }); lazyLoadedAssemblies.AddRange(assemblies); } } catch (Exception ex) { Logger.LogError("Error: {Message}", ex.Message); } } } ``` :::moniker-end Build and run the app again. For the default page that loads the `Index` component (`Pages/Index.razor`), the developer tool's Network tab indicates that the RCL's assembly (`GrantImaharaRobotControls.dll`) does **not** load for the `Index` component: ![Index component loaded in the browser with developer tool's Network tab indicating that the GrantImaharaRobotControls.dll assembly isn't loaded.](~/blazor/webassembly-lazy-load-assemblies/_static/screenshot2.png) If the `Robot` component from the RCL is requested at `/robot`, the `GrantImaharaRobotControls.dll` assembly is loaded and the `Robot` component is rendered: ![Robot component loaded in the browser with developer tool's Network tab indicating that the GrantImaharaRobotControls.dll assembly is loaded.](~/blazor/webassembly-lazy-load-assemblies/_static/screenshot3.png) ## Troubleshoot * If unexpected rendering occurs, such as rendering a component from a previous navigation, confirm that the code throws if the cancellation token is set. * If assemblies configured for lazy loading unexpectedly load at app start, check that the assembly is marked for lazy loading in the project file. :::moniker range="< aspnetcore-6.0" > [!NOTE] > A known issue exists for loading types from a lazily-loaded assembly. For more information, see [Blazor WebAssembly lazy loading assemblies not working when using @ref attribute in the component (dotnet/aspnetcore #29342)](https://github.com/dotnet/aspnetcore/issues/29342). :::moniker-end ## Additional resources * [Handle asynchronous navigation events with `OnNavigateAsync`](xref:blazor/fundamentals/routing#handle-asynchronous-navigation-events-with-onnavigateasync) *