-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone
Description
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
JsonTypeInforetrieved from callingJsonSerializerOptions.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
PublishAotorPublishTrimmedis set, we enable to new feature switch (throwing outMicrosoft.AspNetCore.EnsureJsonTrimmabilityas an example name). This switch would change the behavior of ASP.NET Core when it createdJsonSerializerOptionsinstances. When the switch is "on", theJsonSerializerOptionscreated would not allow Reflection based serialization. Instead, the source generator must be used in the application.
Metadata
Metadata
Assignees
Labels
old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels