Eliminate Trim/AOT Warnings related to System.Text.Json #45527
Labels
old-area-web-frameworks-do-not-use
*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone
When publishing a NativeAOT ASP.NET app, I am getting a lot of trim and AOT warnings because ASP.NET code is (de)serializing objects to/from JSON without using the "trim/aot safe" JSON APIs that take a
JsonTypeInfo
.Here's an example of the warnings I get:
We should eliminate these warnings in ASP.NET and take advantage of the JSON source generator.
One approach we could take:
JsonTypeInfo
retrieved from callingJsonSerializerOptions.GetTypeInfo
.This will push the warnings to the places where
JsonSerializerOptions
is created. For example, you can't useJsonSerializerOptions.Default
ornew DefaultJsonTypeInfoResolver()
, since they are marked asRequiresUnreferencedCode
andRequiresDynamicCode
.PublishAot
orPublishTrimmed
is set, we enable to new feature switch (throwing outMicrosoft.AspNetCore.EnsureJsonTrimmability
as an example name). This switch would change the behavior of ASP.NET Core when it createdJsonSerializerOptions
instances. When the switch is "on", theJsonSerializerOptions
created would not allow Reflection based serialization. Instead, the source generator must be used in the application.The text was updated successfully, but these errors were encountered: