-
Notifications
You must be signed in to change notification settings - Fork 809
HybridCache : simplify AOT usage with default JSON serialization #6475
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
base: main
Are you sure you want to change the base?
Conversation
2. add new extension methods to register JsonSerializerOptions 3. offer a suitable error message instead, if none supplied in AOT mode open question: naming: With* or Add* ?
CI build fail is because of API delta:
This in in turn tied into the open "which API review process should this follow?" question; however, I'll add the attribute for now |
src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/DefaultJsonSerializerFactory.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/DefaultJsonSerializerFactory.cs
Outdated
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Caching.Hybrid/HybridCacheBuilderExtensions.cs
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.Caching.Hybrid/Internal/DefaultJsonSerializerFactory.cs
Show resolved
Hide resolved
This should be able to resolve Microsoft.Extensions.Caching.Hybrid is not trim/AOT compatible (dotnet/extensions#5624). |
We should remove Lines 20 to 21 in 6f29ab5
as well. We should make the library trimming and AOT compatible. |
</PropertyGroup> | ||
|
||
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))"> | ||
<IsTrimmable>true</IsTrimmable> | ||
<IsAotCompatible>true</IsAotCompatible> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
</PropertyGroup> | |
<PropertyGroup Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))"> | |
<IsTrimmable>true</IsTrimmable> | |
<IsAotCompatible>true</IsAotCompatible> | |
<IsAotCompatible Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net7.0'))">true</IsAotCompatible> |
You only need to set IsAotCompatible
. It will default IsTrimmable
. See https://github.com/dotnet/sdk/blob/fd301ec19c3190b8129b28847d6faed9d26bc0d3/src/Tasks/Microsoft.NET.Build.Tasks/targets/Microsoft.NET.Publish.targets#L24
[UnconditionalSuppressMessage("AOT", "IL2026", Justification = "Checked at runtime, guidance issued")] | ||
[UnconditionalSuppressMessage("AOT", "IL2070", Justification = "Checked at runtime, guidance issued")] | ||
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Checked at runtime, guidance issued")] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be moved even more local to only suppress around the lines of code that need it. Doing it the current way will suppress it for all new code written in this class, which isn't correct.
Also, is there a better Justifcation that can be written here? A little more of a reason why this is OK.
See
- https://github.com/dotnet/aspnetcore/blob/5678f227fafa5dcd10b98801d4da54f1add7de6d/src/Http/Http.Results/src/HttpResultsHelper.cs#L22-L24
extensions/src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs
Lines 41 to 57 in 6f29ab5
[UnconditionalSuppressMessage("AotAnalysis", "IL3050", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "DefaultJsonTypeInfoResolver is only used when reflection-based serialization is enabled")] private static JsonSerializerOptions CreateDefaultOptions() { // Copy configuration from the source generated context. JsonSerializerOptions options = new(JsonContext.Default.Options) { Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping, }; if (JsonSerializer.IsReflectionEnabledByDefault) { // If reflection-based serialization is enabled by default, use it as a fallback for all other types. // Also turn on string-based enum serialization for all unknown enums. options.TypeInfoResolverChain.Add(new DefaultJsonTypeInfoResolver()); options.Converters.Add(new JsonStringEnumConverter()); }
for some examples.
JsonSerializerOptions.Default
in AOT modeJsonSerializerOptions
(these are convenience methods on an existing registration pattern)[UnconditionalSuppressMessage]
to prevent build-time consumer warningsThis has been tested successfully with an AOT published project; it now works correctly at runtime with zero build-time warnings, test code shown below; without the
WithJsonSerializerOptions
line, exception at runtime:Without this work, it is not obvious how to register AOT JSON options, and the consumer must use
<NoWarn>$(NoWarn);IL2026;IL2104;IL3053</NoWarn>
or similar to get a clean build, which is too wide.open questions:
With*
orAdd*
?Sample:
Microsoft Reviewers: Open in CodeFlow