Default Blazor WASM project should not produce trim warnings
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 5h
- Merged PRs (30d)
- 276
Description
### Is there an existing issue for this?
- [X] I have searched the existing issues
### Describe the bug
The default project templates for Blazor WASM apps use trimming, but trimming warnings are suppressed by default. Enabling trim warnings shows there are a number of issues with how the API is annotated.
Unfortunately, there isn't guidance available on how to actually (correctly) fix the issues pointed to by these warnings.
Assuming that the application works correctly when trimmed in all cases, it means one or more of:
- the underlying APIs are incorrectly annotated,
- the underlying APIs or app are not suitable for trimming,
- the app produced by the default app template might break at runtime,
- the user is expected to fix the warnings, which are disabled by default, and there isn't sufficient guidance for them to do so (using the [guidance referenced by the warnings](https://aka.ms/dotnet-illink/libraries) the user won't be able to solve the issues correctly),
- warnings might be invalid.
### Expected Behavior
The default project templates (at least) should be able to be published without trim warnings or there should be guidance on how to fix the warnings and point out in the documentation that this is the case (the default template isn't suitable for trimming).
### Steps To Reproduce
```
dotnet new blazorwasm
dotnet publish -p:SuppressTrimAnalysisWarnings=false
```
### Exceptions (if any)
All of the following warnings are issued.
```
ILLink : Trim analysis warning IL2111: BlazorTrimTest.App.<>c.b__0_1(RenderTreeBuilder): Method 'Microsoft.AspNetCore.Components.LayoutView.WrapInLayout(Type, RenderFragment)' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements o
f the method. [~\BlazorTrimTest\BlazorTrimTest.csproj]
ILLink : Trim analysis warning IL2111: BlazorTrimTest.App.<>c.b__0_1(RenderTreeBuilder): Method 'Microsoft.AspNetCore.Components.LayoutView.Layout.set' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method. [~\BlazorTrimTest\BlazorTrimTest.csproj]
ILLink : Trim analysis warning IL2118: BlazorTrimTest.App.<>c.b__0_1(RenderTreeBuilder): Compiler-generated member 'Microsoft.AspNetCore.Components.LayoutView.<>c__DisplayClass13_0.g__Render|0(RenderTreeBuilder)' is accessed via reflection. Trimmer can't guarantee availability of the requirements of the member. [~\BlazorTrimTest\BlazorTrimTest.csproj]
ILLink : Trim analysis warning IL2111: BlazorTrimTest.App.<>c.b__0_1(RenderTreeBuilder): Method 'Microsoft.AspNetCore.Components.LayoutView.Layout.set' with parameters or return value with `DynamicallyAccessedMembersAttribute` is accessed via reflection. Trimmer can't guarantee availability of the requirements of the method. [~\BlazorTrimTest\BlazorTrimTest.csproj]
ILLink : Trim analysis warning IL2110: BlazorTrimTest.App.<>c.b__0_1(RenderTreeBuilder): Field 'Microsoft.AspNetCore.Components.LayoutView.k__BackingField' with 'DynamicallyAccessedMembersAttribute' is accessed via reflection. Trimmer can't guarantee availability of the requirements of the field. [~\BlazorTrimTest\BlazorTrimTest.csproj]
~\BlazorTrimTest\Pages\FetchData.razor(44,9): Trim analysis warning IL2026: BlazorTrimTest.Pages.FetchData.d__2.MoveNext(): Using member 'System.Net.Http.Json.HttpClientJsonExtensions.GetFromJsonAsync(HttpClient, String, CancellationToken)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved. [~\BlazorTrimTest\BlazorTrimTest.csproj]
ILLink : Trim analysis warning IL2118: BlazorTrimTest.App.BuildRenderTree(RenderTreeBuilder): Compiler-generated member 'Microsoft.AspNetCore.Components.Routing.Router.d__61.path' is accessed via reflection. Trimmer can't guarantee availability of the requirements of the member. ~\BlazorTrimTest\BlazorTrimTest.csproj]
~...\.nuget\packages\microsoft.aspnetcore.components\7.0.0-rc.2.22476.2\lib\net7.0\Microsoft.AspNetCore.Components.dll : warning IL2104: Assembly 'Microsoft.AspNetCore.Components' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [~\BlazorTrimTest\BlazorTrimTest.csproj]
~...\.nuget\packages\microsoft.jsinterop\7.0.0-rc.2.22476.2\lib\net7.0\Microsoft.JSInterop.dll : warning IL2104: Assembly 'Microsoft.JSInterop' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries [~\BlazorTrimTest\BlazorTrimTest.csproj]
```
### .NET Version
7.0.100-rc.2.22477.23
### Anything else?
Marginally related: #39839
Contributor guide
Assessment
This issue has not been assessed yet.