Understanding Lazy Load Not Trimmable Assembly AOT Tech Support
In this article, we will discuss the lazy loading of assemblies in ASP.NET Core Blazor WebAssembly and the concept of non-trimmable assemblies. We will also cover the necessary tech support for achieving lazy loading of not trimmable assemblies in Blazor WebAssembly applications.
What is Lazy Loading in ASP.NET Core Blazor WebAssembly?
Lazy loading is a performance optimization technique where an application defers the loading of resources until they are required. ASP.NET Core Blazor WebAssembly supports lazy loading of assemblies through the Microsoft.Extensions.DependencyModel package. This allows developers to specify the assemblies that can be loaded on-demand, reducing the initial payload of the application.
What is a Not Trimmable Assembly?
A not trimmable assembly is an assembly that the .NET runtime cannot remove during the trimming process. Trimming is a process where the .NET runtime analyzes the application and removes unused assemblies, types, and members, resulting in a smaller deployment package. However, some assemblies, types, and members are considered essential by the .NET runtime, and they cannot be removed. These are referred to as not trimmable assemblies.
Why is Tech Support Necessary for Lazy Loading Not Trimmable Assemblies?
Tech support is necessary for lazy loading not trimmable assemblies because these assemblies need to be present in the application for it to function correctly. However, since they cannot be removed during trimming, they increase the initial payload of the application. By implementing a lazy loading strategy, developers can reduce the initial payload and improve the performance of the application.
Implementing Lazy Loading of Not Trimmable Assemblies in ASP.NET Core Blazor WebAssembly
To implement lazy loading of not trimmable assemblies in ASP.NET Core Blazor WebAssembly, follow these steps:
- Create a new ASP.NET Core Blazor WebAssembly application using the command
dotnet new blazorwasm -o MyApp. - Add the
Microsoft.Extensions.DependencyModelpackage to the application using the commanddotnet add package Microsoft.Extensions.DependencyModel. - Create a new class library project using the command
dotnet new classlib -o MyLib. - Add the
Microsoft.Extensions.DependencyModelpackage to the class library project. - Define a type in the class library project that will be used for lazy loading.
- Modify the
Program.csfile in the WebAssembly project to use the lazy loading strategy.using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyModel; var builder = WebAssemblyHostBuilder.CreateDefault(args); builder.RootComponents.Add("#app"); // Configure services builder.Services.AddTransient(serviceProvider => { var assembly = typeof(Program).Assembly; varAssemblyLoadContext loadContext = new AssemblyLoadContext("MyLoadContext", true); var depsModel = DependencyContext.Load(assembly); var assemblies = depsModel.RuntimeLibraries.Where(library => library.Type == "project" && library.Name == "MyLib").Select(library => loadContext.LoadFromStream(new MemoryStream(File.ReadAllBytes(library.Name + ".dll")))).ToArray(); loadContext.Unload(); return new Lazy<IService>(() => new MyLib.MyType()); }); builder.Build().Run(); References
- Lazy loading assemblies in ASP.NET Core Blazor WebAssembly
- Trim Self-contained Deployments
- #23910 Lazy loading in Blazor WebAssembly
- #38278 Trimming doesn't exclude non-trimmable references
This article covered the topic of lazy loading not trimmable assemblies in ASP.NET Core Blazor WebAssembly. By understanding the concept of lazy loading and not trimmable assemblies, developers can optimize the performance of their applications and reduce initial payloads. Follow the steps outlined in this article to implement lazy loading of not trimmable assemblies in your ASP.NET Core Blazor WebAssembly applications.