Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate Trim/AOT Warnings related to System.Text.Json #45527

Closed
eerhardt opened this issue Dec 9, 2022 · 1 comment · Fixed by #46008
Closed

Eliminate Trim/AOT Warnings related to System.Text.Json #45527

eerhardt opened this issue Dec 9, 2022 · 1 comment · Fixed by #46008
Assignees
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone

Comments

@eerhardt
Copy link
Member

eerhardt commented Dec 9, 2022

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:

ILC : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.HttpResultsHelper.WriteResultAsJsonAsync<T>(HttpContext,ILogger,!!0,String,JsonSerializerOptions): Using member 'Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsync<T>(HttpResponse,T,JsonSerializerOptions,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. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]
ILC : Trim analysis warning IL2026: Microsoft.AspNetCore.Http.HttpResultsHelper.WriteResultAsJsonAsync<T>(HttpContext,ILogger,!!0,String,JsonSerializerOptions): Using member 'Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsync(HttpResponse,Object,Type,JsonSerializerOptions,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. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]
ILC : AOT analysis warning IL3050: Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsync<TValue>(HttpResponse,!!0,JsonSerializerOptions,String,CancellationToken): Using member 'System.Text.Json.JsonSerializer.SerializeAsync<TValue>(Stream,TValue,JsonSerializerOptions,CancellationToken)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]
ILC : AOT analysis warning IL3050: Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.WriteAsJsonAsync(HttpResponse,Object,Type,JsonSerializerOptions,String,CancellationToken): Using member 'System.Text.Json.JsonSerializer.SerializeAsync(Stream,Object,Type,JsonSerializerOptions,CancellationToken)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]
ILC : AOT analysis warning IL3050: Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.<WriteAsJsonAsyncSlow>d__9.MoveNext(): Using member 'System.Text.Json.JsonSerializer.SerializeAsync(Stream,Object,Type,JsonSerializerOptions,CancellationToken)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]
ILC : AOT analysis warning IL3050: Microsoft.AspNetCore.Http.HttpResponseJsonExtensions.<WriteAsJsonAsyncSlow>d__5`1.MoveNext(): Using member 'System.Text.Json.JsonSerializer.SerializeAsync<TValue>(Stream,TValue,JsonSerializerOptions,CancellationToken)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]
ILC : AOT analysis warning IL3050: Microsoft.AspNetCore.Http.HttpRequestJsonExtensions.<ReadFromJsonAsync>d__2`1.MoveNext(): Using member 'System.Text.Json.JsonSerializer.DeserializeAsync<TValue>(Stream,JsonSerializerOptions,CancellationToken)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications. [C:\git\Albums\AlbumsNetCore\AlbumsNetCore.csproj]

We should eliminate these warnings in ASP.NET and take advantage of the JSON source generator.

One approach we could take:

  • Everywhere we call JsonSerializer, we always pass in a JsonTypeInfo retrieved from calling JsonSerializerOptions.GetTypeInfo.

This will push the warnings to the places where JsonSerializerOptions is created. For example, you can't use JsonSerializerOptions.Default or new DefaultJsonTypeInfoResolver(), since they are marked as RequiresUnreferencedCode and RequiresDynamicCode.

  • When PublishAot or PublishTrimmed is set, we enable to new feature switch (throwing out Microsoft.AspNetCore.EnsureJsonTrimmability as an example name). This switch would change the behavior of ASP.NET Core when it created JsonSerializerOptions instances. When the switch is "on", the JsonSerializerOptions created would not allow Reflection based serialization. Instead, the source generator must be used in the application.
@javiercn javiercn added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Dec 12, 2022
@captainsafia captainsafia added this to the .NET 8 Planning milestone Dec 13, 2022
@ghost
Copy link

ghost commented Dec 13, 2022

Thanks for contacting us.

We're moving this issue to the .NET 8 Planning milestone for future evaluation / consideration. We would like to keep this around to collect more feedback, which can help us with prioritizing this work. We will re-evaluate this issue, during our next planning meeting(s).
If we later determine, that the issue has no community involvement, or it's very rare and low-impact issue, we will close it - so that the team can focus on more important and high impact issues.
To learn more about what to expect next and how this issue will be handled you can read more about our triage process here.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
No open projects
Status: Done
Development

Successfully merging a pull request may close this issue.

4 participants