Skip to content

Commit 2339991

Browse files
committed
Add DynamicDependencyAttribute to prevent LazyAssemblyBuilder from being trimmed
1 parent 80cf245 commit 2339991

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

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

Lines changed: 8 additions & 3 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.Diagnostics.CodeAnalysis;
45
using System.Reflection;
56
using Microsoft.AspNetCore.Components;
67
using Microsoft.AspNetCore.Components.Authorization;
@@ -15,6 +16,9 @@ namespace Microsoft.Extensions.DependencyInjection;
1516
/// </summary>
1617
public static class WebAssemblyRazorComponentsBuilderExtensions
1718
{
19+
private const string LazyAssemblyLoaderAssemblyName = "Microsoft.AspNetCore.Components.WebAssembly";
20+
private const string LazyAssemblyLoaderTypeName = "Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader";
21+
1822
/// <summary>
1923
/// Adds services to support rendering interactive WebAssembly components.
2024
/// </summary>
@@ -27,7 +31,7 @@ public static IRazorComponentsBuilder AddInteractiveWebAssemblyComponents(this I
2731
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<RenderModeEndpointProvider, WebAssemblyEndpointProvider>());
2832

2933
// Try register LazyAssemblyLoader to prevent crashes during prerendering.
30-
// TODO: Remove this once LazyAssemblyLoader is deprecated.
34+
// TODO: Remove this once LazyAssemblyLoader is no longer used.
3135
var lazyAssemblyLoaderType = GetLazyAssemblyLoaderType();
3236

3337
if (lazyAssemblyLoaderType != null)
@@ -57,12 +61,13 @@ public static IRazorComponentsBuilder AddAuthenticationStateSerialization(this I
5761
return builder;
5862
}
5963

64+
[DynamicDependency(DynamicallyAccessedMemberTypes.PublicConstructors, LazyAssemblyLoaderTypeName, LazyAssemblyLoaderAssemblyName)]
6065
private static Type? GetLazyAssemblyLoaderType()
6166
{
6267
try
6368
{
64-
var assembly = Assembly.Load("Microsoft.AspNetCore.Components.WebAssembly");
65-
return assembly.GetType("Microsoft.AspNetCore.Components.WebAssembly.Services.LazyAssemblyLoader", throwOnError: false);
69+
var assembly = Assembly.Load(LazyAssemblyLoaderAssemblyName);
70+
return assembly.GetType(LazyAssemblyLoaderTypeName, throwOnError: false);
6671
}
6772
catch
6873
{

0 commit comments

Comments
 (0)