Blazor 8 trimming: how to remove IL warnings from components and platforms methods

72 Views Asked by At

While migrating to net 8.0, we've enabled partial trimming on the blazor project. We've also opted for disabling trimming for our rest service helpers since they use reflection (migration to source based generation isn't possible right now).

Here's the <PropertyGroup> entry for the Blazor project:

<PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>
    <Nullable>enable</Nullable>
    <ImplicitUsings>enable</ImplicitUsings>
    <PublishTrimmed>true</PublishTrimmed>
    <TrimMode>partial</TrimMode>
    <SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
    <LangVersion>latest</LangVersion>
    <MSBuildWarningsAsMessages>NU1702</MSBuildWarningsAsMessages>        
</PropertyGroup>

If I read the docs right, I can use the <TrimRootAssembly> to mark an assembly as as non trimmable. So, I've added an entry like this for the rest service helpers:

<ItemGroup>
    <TrimmerRootAssembly Include="Grm.Assistencias.ClientServices" />
</ItemGroup>

The library itself (which is also in net 8.0) is marked as non trimmable (the library is used by other projects which are stil on net 4.8 and we're using multitargetting):

false enable

I was under the impression that this should be enough for the compiler to stop generating warnings for my library's code, but the truth is that I'm still getting warnings like the following:

ServicoRestBase.cs(102,9): Trim analysis wa
rning IL2026: Grm.Assistencias.ClientServices.ServicoRestBase.<DeserializeAsync>d__13<T>.MoveNext(): Using member 'Syst
em.Net.Http.Json.HttpContentJsonExtensions.ReadFromJsonAsync<T>(HttpContent, JsonSerializerOptions, CancellationToken)'
 which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. JSON serializati
on and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeIn
fo or JsonSerializerContext, or make sure all of the required types are preserved. [D:\code\work\tfsonline\Assistencias
Helpdesk\Grm.Assistencias.Web\Grm.Assistencias.Web.csproj]

Even though the code does work at runtime (it seems like nothing has been suppressed from my types when the app is published as self-container), I'd really love to know if there's anything I can do to keep the compiler quite on this call.

Another issue we're having is that we end up getting lots of IL2091 warnings while using the out-of-the-box controls provider by the framework. For instance, we're getting this warning when creating a InputText control on a simple form:

ILLink : Trim analysis warning IL2091: __Blazor.Grm.Assistencias.Web.Pages.Secured.Pedidos.Components.PedidoEditarDados
Gerais.TypeInference.CreateInputSelect_4<TValue>(RenderTreeBuilder, Int32, Int32, Object, Int32, Object, Int32, TValue,
 Int32, EventCallback<TValue>, Int32, Expression<Func<TValue>>, Int32, RenderFragment): 'TValue' generic argument does
not satisfy 'DynamicallyAccessedMemberTypes.All' in 'Microsoft.AspNetCore.Components.Forms.InputSelect<TValue>'. The ge
neric parameter 'TValue' of '__Blazor.Grm.Assistencias.Web.Pages.Secured.Pedidos.Components.PedidoEditarDadosGerais.Typ
eInference.CreateInputSelect_4<TValue>(RenderTreeBuilder, Int32, Int32, Object, Int32, Object, Int32, TValue, Int32, Ev
entCallback<TValue>, Int32, Expression<Func<TValue>>, Int32, RenderFragment)' does not have matching annotations. The s
ource value must declare at least the same requirements as those declared on the target location it is assigned to. [D:
\code\work\tfsonline\AssistenciasHelpdesk\Grm.Assistencias.Web\Grm.Assistencias.Web.csproj]

This code is generated during the build by the razor compiler, so I think that there's no way for me to add the requested attribute (DynamicallyAccessedMemberTypes.All) to that method. How can I make the compiler happy with this?

Thanks.

0

There are 0 best solutions below