Skip to content

Commit ed81b3d

Browse files
committed
Try register LazyAssemblyLoader to DI in AddInteractiveWebAssemblyComponents
1 parent dc92b65 commit ed81b3d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

src/Components/WebAssembly/Server/src/WebAssemblyRazorComponentsBuilderExtensions.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using System.Reflection;
45
using Microsoft.AspNetCore.Components;
56
using Microsoft.AspNetCore.Components.Authorization;
67
using Microsoft.AspNetCore.Components.Endpoints.Infrastructure;
@@ -25,6 +26,15 @@ public static IRazorComponentsBuilder AddInteractiveWebAssemblyComponents(this I
2526

2627
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<RenderModeEndpointProvider, WebAssemblyEndpointProvider>());
2728

29+
// Try register LazyAssemblyLoader to prevent crashes during prerendering.
30+
// TODO: Remove this once LazyAssemblyLoader is deprecated.
31+
var lazyAssemblyLoaderType = GetLazyAssemblyLoaderType();
32+
33+
if (lazyAssemblyLoaderType != null)
34+
{
35+
builder.Services.AddScoped(lazyAssemblyLoaderType);
36+
}
37+
2838
return builder;
2939
}
3040

@@ -46,4 +56,17 @@ public static IRazorComponentsBuilder AddAuthenticationStateSerialization(this I
4656

4757
return builder;
4858
}
59+
60+
private static Type? GetLazyAssemblyLoaderType()
61+
{
62+
try
63+
{
64+
var assembly = Assembly.Load("Microsoft.AspNetCore.Components.WebAssembly");
65+
return assembly.GetType("Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader", throwOnError: false);
66+
}
67+
catch
68+
{
69+
return null;
70+
}
71+
}
4972
}

0 commit comments

Comments
 (0)