-
Notifications
You must be signed in to change notification settings - Fork 18
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
Support Native AOT #1348
base: main
Are you sure you want to change the base?
Support Native AOT #1348
Conversation
This commit gets AoT working to the point that I can actually test it, and see it work AOT locally
This might be a bit cleaner to integrate if we look to add .NET 8 as a first-class TFM first |
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #1348 +/- ##
==========================================
- Coverage 79.43% 78.70% -0.74%
==========================================
Files 138 142 +4
Lines 3234 3306 +72
Branches 450 462 +12
==========================================
+ Hits 2569 2602 +33
- Misses 434 468 +34
- Partials 231 236 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
We use this test project in Polly. In theory compiling it with the assemblies trim-rooted is enough to flush out issues that the analysers can't detect.
Unless we have to break stuff, I'd class it just as a minor. I'd a minor planned for #1335 once I've validated it internally, so depending on how long both take to complete, we could have either an 8.2 then an 8.3, or ship them together as 8.2. |
src/JustSaying.Extensions.DependencyInjection.Microsoft/IServiceCollectionExtensions.cs
Outdated
Show resolved
Hide resolved
...ons.DependencyInjection.Microsoft/JustSaying.Extensions.DependencyInjection.Microsoft.csproj
Show resolved
Hide resolved
public override string ToString() | ||
#if NET8_0_OR_GREATER | ||
=> System.Text.Json.JsonSerializer.Serialize(this, JustSayingSerializationContext.Default.RedrivePolicy); |
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.
this
might be problematic for AoT in case it's a derived type.
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.
Yeah, that's a good point, I think it'll end up serializing the base fields, but none added by the derived type.
At the moment, RedrivePolicy
is only used internally, and I can't see a way that an API user could derive it and pass it through to any internal usage, but I could be missing it.
If that is the case, I don't think it would be a bad idea to internal sealed
it.
if (RuntimeFeature.IsDynamicCodeSupported) | ||
{ | ||
#pragma warning disable IL2026 | ||
#pragma warning disable IL3050 | ||
return new NewtonsoftSerializationFactory(); | ||
#pragma warning restore | ||
} |
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.
Slightly surprised it isn't smart enough to not warn about this.
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.
I'll check, I think I had the check backwards when I added the #pragma warning disable
's
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.
Yeah, even with that fixed, it still warns.
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.
I've found an issue related to this: dotnet/runtime#97273, looks like the supression in runtime checks just didn't make it into .NET 8, not sure if they plan to backport either.
It also looks like there are plans for a wider design proposal to support feature checks suppressing analyzers: dotnet/runtime#96859
var dataType = obj.Value.GetProperty("Type").GetString(); | ||
var dataValue = obj.Value.GetProperty("Value").GetString(); |
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.
We should make these defensive against the possibility of the properties not being there so we don't NRE.
src/JustSaying/Messaging/MessageSerialization/SystemTextJsonSerializer`1.cs
Show resolved
Hide resolved
|
||
namespace JustSaying.Messaging.MessageSerialization; | ||
|
||
public class TypedSystemTextJsonSerializationFactory(JsonSerializerOptions options) : IMessageSerializationFactory |
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.
Not the biggest fan of the Typed
prefix.
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.
Yeah, same, I was at the "get it working at any cost" stage by then. I'll try and think of something a bit sleaker, or merge this back in with the current STJSerializationFactory, and have it use the typed serializer when dynamic code isn't supported.
If we go this way, in the next breaking version, I think it'll make sense to deprecate the non-typed serializer, and to that end, maybe decorate it now.
var typeInfo = options.GetTypeInfo(typeof(T)); | ||
if (typeInfo is not JsonTypeInfo<T> genericTypeInfo) | ||
{ | ||
throw new JsonException($"Could not find type info for the specified type {typeof(T).Name}"); |
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.
This could probably be a bit more informative.
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.
It ends up being redundant anyway tbh, GetTypeInfo(Type)
throws if it can't find the type info anyway.
That makes sense. It makes sense to me that we should add the |
(Pushing rather than stashing)
This is looking very nice 🙂 I had looked at this a while back in a local branch and the thing that I was concerned about was the |
Yeah, I'd agree, I think this is a known enough sharp edge of AOT compilation, that we could just guard the addition of the default |
Another thought I had while browsing this change, now might be the time to create separate interfaces and implementation for the "message envelope" and message serializer/deserializer. |
src/JustSaying/Messaging/MessageSerialization/JustSayingJsonSerializerOptions.cs
Outdated
Show resolved
Hide resolved
Yeah, I had originally thought this change would require doing that anyway but found a way to avoid it. |
#if NET8_0_OR_GREATER | ||
[RequiresUnreferencedCode(Constants.SerializationUnreferencedCodeMessage)] | ||
[RequiresDynamicCode(Constants.SerializationDynamicCodeMessage)] | ||
#endif | ||
public class NewtonsoftSerializer : IMessageSerializer |
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.
This is an assumption at the moment. Newtonsoft.Json isn't currently annotated for AOT/Trimming compatibility.
src/JustSaying/Messaging/MessageSerialization/SystemTextJsonSerializer.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: Martin Costello <[email protected]>
This PR is a work in progress to get Native AoT working. Posting early, as I think it'll be good to get eyes on early.
AoT support as added by using a new generic serializer type and serialization factory to create it.
fixes #1347 (eventually)
Things still to do:
Microsoft.Extensions.Options