-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Description
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
Content source URL
Document ID
1487b5e7-a40c-d757-c530-d0e4017f189e
Platform Id
4300acf4-0cea-1a83-bb85-7e567889b895
Article author
Metadata
- ID: 2203b81b-1b37-8599-eb50-f6284170d224
- PlatformId: 4300acf4-0cea-1a83-bb85-7e567889b895
- Service: aspnet-core
- Sub-service: blazor
Metadata
Metadata
Assignees
Labels
Type
Projects
Status