Skip to content

.NET 8 blazor .wasm Lazy Loaded Assemblies throws runtime exception and fails to load #95381

Closed

Description

@fingers10

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I have my production blazor wasm app running on .NET 7 with lazy loading enabled. Now I'm updating my blazor wasm app to .NET 8 following the docs. And when I run, I'm surprised to see a run time exception and the assemblies are not loaded.

Steps I did:

  1. Updated all the <BlazorWebAssemblyLazyLoad Include="{ASSEMBLY}.dll" /> in Web.csproj to <BlazorWebAssemblyLazyLoad Include="{ASSEMBLY}.wasm" />
  2. Updated all the await AssemblyLoader.LoadAssembliesAsync(new[] { "{ASSEMBLY}.dll" }); in App.razor to await AssemblyLoader.LoadAssembliesAsync(new[] { "{ASSEMBLY}.wasm" });

Expected Behavior

The app should lazy load the assemblies and run them like before in .NET 7. Here is the working main branch of ilovedotnet repo.

Steps To Reproduce

  1. Clone main branch of ilovedotnet repo.
  2. Go to commit a6a5dca35abe1b9182f6c497a34d03a5635760bc.
  3. Update to .NET 8.
  4. Run using VisualStudio 2022 17.8.1 or dotnet run command.
  5. Access from browser.
    image
  6. Clear Site Data under Application Tab in browser.
  7. Reload the home page and navigate to any article
    image

Exceptions (if any)

Runtime Error:

Unhandled exception rendering component:  Could not load file or assembly 'Components, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. 
 System.IO.FileNotFoundException: Could not load file or assembly 'Components, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies.
IO_FileName_Name, Components, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
   at Microsoft.AspNetCore.Components.ComponentBase.<.ctor>b__6_0(RenderTreeBuilder builder)
   at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder , RenderFragment , Exception& )

Error Screen shot:
image

.NET Version

8.0.100

Anything else?

dotnet --info:

.NET SDK:
 Version:           8.0.100
 Commit:            57efcf1350
 Workload version:  8.0.100-manifests.8d38d0cc

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22000
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100\

.NET workloads installed:
 Workload version: 8.0.100-manifests.8d38d0cc
 [wasm-tools-net7]
   Installation Source: SDK 8.0.100
   Manifest Version:    8.0.0/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.net7\8.0.0\WorkloadManifest.json
   Install Type:              Msi

 [ios]
   Installation Source: VS 17.8.34316.72
   Manifest Version:    17.0.8478/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8478\WorkloadManifest.json
   Install Type:              Msi

 [android]
   Installation Source: VS 17.8.34316.72
   Manifest Version:    34.0.43/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.43\WorkloadManifest.json
   Install Type:              Msi

 [maui-windows]
   Installation Source: VS 17.8.34316.72
   Manifest Version:    8.0.3/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json
   Install Type:              Msi

 [wasm-tools]
   Installation Source: VS 17.8.34316.72
   Manifest Version:    8.0.0/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.0\WorkloadManifest.json
   Install Type:              Msi

 [maccatalyst]
   Installation Source: VS 17.8.34316.72
   Manifest Version:    17.0.8478/8.0.100
   Manifest Path:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8478\WorkloadManifest.json
   Install Type:              Msi


Host:
  Version:      8.0.0
  Architecture: x64
  Commit:       5535e31a71

.NET SDKs installed:
  8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.4 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Visual Studio:
Microsoft Visual Studio Enterprise 2022 (64-bit) - Current
Version 17.8.1

Windows:
Edition Windows 11 Pro
Version 21H2
Installed on ‎10-‎11-‎2021
OS build 22000.2538
Experience Windows Feature Experience Pack 1000.22001.1000.0

Activity

  1. changed the title [-].NET 8 .wasm Lazy Loaded Assemblies throws runtime exception and fails to load[/-] [+].NET 8 blazor .wasm Lazy Loaded Assemblies throws runtime exception and fails to load[/+] on Nov 26, 2023
  2. fingers10 commented on Nov 27, 2023

    @fingers10
    Author

    @mkArtakMSFT I have added the repo to reproduce issue in steps to reproduce

  3. Sean4572435243 commented on Nov 28, 2023

    @Sean4572435243

    This helped me; specifically the ToList() addition:
    dotnet/aspnetcore#52162 (comment)

  4. fingers10 commented on Nov 29, 2023

    @fingers10
    Author

    @Sean4572435243 I'm still not able to figure out what I'm missing.

    Here is my .csproj:

    <ItemGroup>
    	<ProjectReference Include="..\BlazorDemoComponents\BlazorDemoComponents.csproj" />
    	<ProjectReference Include="..\Components\Components.csproj" />
    	<ProjectReference Include="..\DependencyInjectionDemoComponents\DependencyInjectionDemoComponents.csproj" />
    	<ProjectReference Include="..\DesignPatternDemoComponents\DesignPatternDemoComponents.csproj" />
    	<ProjectReference Include="..\HTTPClientDemoComponents\HTTPClientDemoComponents.csproj" />
    	<ProjectReference Include="..\LINQDemoComponents\LINQDemoComponents.csproj" />
    	<ProjectReference Include="..\OOPSDemoComponents\OOPSDemoComponents.csproj" />
    	<ProjectReference Include="..\ReportDemoComponents\ReportDemoComponents.csproj" />
    	<ProjectReference Include="..\SharedComponents\SharedComponents.csproj" />
    </ItemGroup>
    
    <ItemGroup>
    	<BlazorWebAssemblyLazyLoad Include="BlazorDemoComponents.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="Components.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="DependencyInjectionDemoComponents.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="DesignPatternDemoComponents.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="HTTPClientDemoComponents.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="LINQDemoComponents.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="OOPSDemoComponents.wasm" />
    	<BlazorWebAssemblyLazyLoad Include="ReportDemoComponents.wasm" />
    </ItemGroup>

    Here is my App.razor:

    @using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
    @using Microsoft.AspNetCore.Components.WebAssembly.Services;
    @implements IAsyncDisposable;
    @inject IWebAssemblyHostEnvironment Environment;
    
    <CascadingValue Value="SmallDevice">
        <Router AppAssembly="@typeof(Program).Assembly" OnNavigateAsync="@OnNavigateAsync">
            <Found Context="routeData">
                <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
                <FocusOnNavigate RouteData="@routeData" Selector="h1" />
            </Found>
            <NotFound>
                <PageTitle>Not found</PageTitle>
                <LayoutView Layout="@typeof(MainLayout)">
                    <div class="[ fixed ] [ top-12 ] [ w-full ] [ h-full ] [ flex flex-col items-center justify-center ] [ space-y-4 ]">
                        <img src="/image/error/error-404.webp" />
                        <section class="[ text-4xl md:text-9xl ]">
                            <span class="[ dark:text-white ]">I</span>
                            <span class="animate-pulse">💔</span>
                            <span class="[ text-indigo-600 ] [ dark:text-white ]">.NET</span>
                        </section>
                        <p class="[ dark:text-white ] [ text-2xl ] [ text-center ]" role="alert">Sorry, there's nothing at this address.</p>
                    </div>
                </LayoutView>
            </NotFound>
        </Router>
        @if (Environment.IsProduction())
        {    
            <Blazor.Analytics.Components.NavigationTracker />
        }
    </CascadingValue>
    
    @code {
        const ushort SMALLDEVICEWIDTH = 640;
    
        private ushort viewPortWidth;
        private IJSObjectReference? module;
        private bool SmallDevice = true;
    
        [Inject] private IJSRuntime JSRuntime { get; set; } = default!;
        [Inject] private LazyAssemblyLoader AssemblyLoader { get; set; } = default!;
        [Inject] private ILogger<App> Logger { get; set; } = default!;
        [Inject] private AppState AppState { get; set; } = default!;
    
        protected override async Task OnAfterRenderAsync(bool firstRender)
        {
            if (firstRender)
            {
                module = await JSRuntime.InvokeAsync<IJSObjectReference>("import", "./js/viewport.js");
    
                viewPortWidth = await module.InvokeAsync<ushort>("getViewPortWidth");
    
                SmallDevice = viewPortWidth < SMALLDEVICEWIDTH;
    
                await InvokeAsync(StateHasChanged);
            }
        }
    
        private async Task OnNavigateAsync(NavigationContext args)
        {
            try
            {
                if (args.Path.Contains("blogs", StringComparison.OrdinalIgnoreCase))
                {
                    HideNavigationMenu();
    
                    await AssemblyLoader.LoadAssembliesAsync(new[] { "Components.wasm" });
    
                    if (args.Path.Contains("blazor", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "BlazorDemoComponents.wasm" });
                    }
    
                    if (args.Path.Contains("oops", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "OOPSDemoComponents.wasm" });
                    }
    
                    if (args.Path.Contains("dependency-injection", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "DependencyInjectionDemoComponents.wasm" });
                    }
    
                    if (args.Path.Contains("http-client", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "HTTPClientDemoComponents.wasm" });
                    }
    
                    if (args.Path.Contains("linq", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "LINQDemoComponents.wasm" });
                    }
    
                    if (args.Path.Contains("report", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "ReportDemoComponents.wasm" });
                    }
    
                    if (args.Path.Contains("design-pattern", StringComparison.OrdinalIgnoreCase))
                    {
                        await AssemblyLoader.LoadAssembliesAsync(new[] { "DesignPatternDemoComponents.wasm" });
                    }
                }
    
                if (args.Path.Contains("talks", StringComparison.OrdinalIgnoreCase))
                {
                    HideNavigationMenu();
    
                    await AssemblyLoader.LoadAssembliesAsync(new[] { "Components.wasm" });
                }
            }
            catch (Exception ex)
            {
                Logger.LogError("Error: {Message}", ex.Message);
            }
        }
    
        private void HideNavigationMenu()
        {
            if (!SmallDevice) 
            {
                AppState.HideNavigationMenu();
            }
        }
    
        async ValueTask IAsyncDisposable.DisposeAsync()
        {
            if (module is not null)
            {
                await module.DisposeAsync();
            }
        }
    }

    I dont have any routable components inside other assemblies. So I didnt add anything in AdditionalAssemblies in Router. Please can you assist me?

  5. fingers10 commented on Nov 29, 2023

    @fingers10
    Author

    I figured it out. The issue in my case is that I was having RCL named Components.csproj. This is causing the trouble. Not sure if the name Components is a reserved word which should not be used as project name. I understood from this issue - #95169 that there is a collision in lazy loading when using certain types of names.

    I renamed my project from Components.csproj to BaseComponents.csproj and updated the references everywhere.

    .csproj:

    <ItemGroup>
    	<ProjectReference Include="..\BaseComponents\BaseComponents.csproj" />
    </ItemGroup>
    
    <ItemGroup>
    	<BlazorWebAssemblyLazyLoad Include="BaseComponents.wasm" />
    </ItemGroup>

    App.razor:

    await AssemblyLoader.LoadAssembliesAsync(new[] { "BaseComponents.wasm" });

    This resolved my issue.

    But this is no where documented on what kind of names should not be used. Or there should be an alternate alias naming option should be provided to overcome this issue.

  6. javiercn commented on Nov 29, 2023

    @javiercn
    Member

    @fingers10 thanks for the additional details.

    I'm not sure how the rename has helped/fixed anything in this case. There are no "reserved" names that shouldn't be used afaik that would not cause your c# compilation to fail.

    I'm moving this to the runtime in case they want to take a look, but it doesn't seem that this is an issue in the framework, rather something in the app/upgrade process.

  7. transferred this issue fromdotnet/aspnetcoreon Nov 29, 2023
  8. ghost added
    needs-area-label
    on Nov 29, 2023
  9. ghost added
    untriaged
    on Nov 29, 2023
  10. added
    arch-wasm
    on Nov 29, 2023
  11. removed
    needs-area-label
    on Nov 29, 2023
  12. added this to the 9.0.0 milestone on Nov 29, 2023
  13. ghost removed
    untriaged
    on Nov 29, 2023
  14. added
    os-browser
    on Nov 29, 2023
  15. removed this from the 9.0.0 milestone on Nov 29, 2023
  16. ghost added
    untriaged
    on Nov 29, 2023
  17. added this to the 8.0.x milestone on Dec 13, 2023
  18. ghost removed
    untriaged
    on Dec 13, 2023
  19. maraf commented on Dec 13, 2023

    @maraf
    Member

    I was able reproduce the issue. The problem is that we are trying to search in already loaded assemblies by "contains" in assembly name, not "exact match"

    if (loaderHelpers.loadedAssemblies.some(f => f.includes(assemblyNameToLoad))) {

    The blazor framework contains Microsoft.AspNetCore.Components.wasm and so Components.wasm gets incorrectly marked as already loaded

  20. fingers10 commented on Dec 13, 2023

    @fingers10
    Author

    Wow @maraf many thanks for the support and findings.

  21. ghost added
    in-pr
    on Dec 14, 2023
  22. ghost removed
    in-pr
    on Jan 17, 2024
  23. locked and limited conversation to collaborators on Feb 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

arch-wasmarea-AssemblyLoader-monoos-browser

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions