Skip to content

Some clarification on this IL Trimming behaviour would be useful. #35170

@StevePy

Description

@StevePy

Description

This page makes sense for the most part but with the example given it is not clear how/why the IL Trimmer would: "When the app is published, KeyValuePair is trimmed from the app, even in spite of setting the property to false in the project file."

It is pretty clear that a call to .Deserialize<List<KeyValuePair<string,string>>>() would not be picked up and preserved by the Trimmer, but would the presence of: private List<KeyValuePair<string, string>> items = []; declaration not have marked KeyValuePair<string, string> as a type not to be trimmed as it was declared explicitly and initialized?

On a side note I came across this article while digging around for info about the same/similar error though with a completely custom type. It worked when debugging, but failed on publish. I did track down the issue via trial and error where a protected default constructor seems to have prevented the class from being included:

    public class ProfileDto : IDto
    {
        public string Key { get; set; }
        public string Name { get; set; }

        public ProfileDto(string key, string name)
        {
            ArgumentException.ThrowIfNullOrEmpty(key, nameof(key));
            ArgumentException.ThrowIfNullOrEmpty(name, nameof(name));
            Key = key;
            Name = name;
        }

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
        protected ProfileDto()
        { }
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

This protected constructor is a pattern I use to manage nullable reference warnings, it just becomes a default as there are often situations especially with EF entities where not every one is passed in a constructor. I had a service class inside the Blazor WASM return a populated ProfileDto (using the public constructor) when I got this error. The error mentioned JSON deserialization in the call stack which seemed "odd". On a hunch I made the default constructor public and presto, it worked without an error. No errors with development build/tests, but failed ugly when published. On a hunch I also tried just removing the default constructor and the same behavior. Without a public default constructor on this DTO a published Blazor app fails with:

Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: ArgumentNull_Generic Arg_ParamName_Name, key
System.ArgumentNullException: ArgumentNull_Generic Arg_ParamName_Name, key
at System.Object..ctor(String , String , Object , Object )
at System.Text.Json.Serialization.JsonConverter1[[Foxtrot.Common.Models.ProfileDto, Foxtrot.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ReadCore(Utf8JsonReader& , ProfileDto& , JsonSerializerOptions , ReadStack& ) at System.Text.Json.JsonSerializer.ReadFromSpan[ProfileDto](ReadOnlySpan1 , JsonTypeInfo1 ) at Blazored.LocalStorage.LocalStorageService.<GetItemAsync>d__51[[Foxtrot.Common.Models.ProfileDto, Foxtrot.Common, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].MoveNext() ....

Was digging into a possible explanation and came across some issues around KeyValuePair and then this article. Can someone elaborate on reasons/scenarios how/why the IL Trimmer might exclude classes where there is non-reflective code referencing types, and when lacking a public default constructor?

Page URL

https://learn.microsoft.com/en-us/aspnet/core/blazor/host-and-deploy/configure-trimmer?view=aspnetcore-9.0

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/blazor/host-and-deploy/configure-trimmer.md

Document ID

1487b5e7-a40c-d757-c530-d0e4017f189e

Platform Id

4300acf4-0cea-1a83-bb85-7e567889b895

Article author

@guardrex

Metadata

  • ID: 2203b81b-1b37-8599-eb50-f6284170d224
  • PlatformId: 4300acf4-0cea-1a83-bb85-7e567889b895
  • Service: aspnet-core
  • Sub-service: blazor

Related Issues

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions