Closed
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
JsonTypeInfo
retrieved 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
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.