-
Notifications
You must be signed in to change notification settings - Fork 14
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
Native Aot Support #135
Comments
So I've done a bit of digging and Serialization options are all internal. I think the crux of this is we need a way of setting the TypeInfoResolver on the SerializerOptions. So that the resolver can be passed from the project specific JsonSerializerContext sub class. The options specified for the JsonSourceGenerationOptions need to match the current options constructed in the DatasyncSerializer class. A JsonSerializable attribute will need to be added for each offline Entity to the JsonSerializerContext sub class it will need to include the Page class as well.
I think my proposal would be to add a property to the OfflineOptions class for the serializer options and add a Method to the DatasyncOfflineOptionsBuilder for UseSerializerOptions. Then use the OfflineOptions property to set the JsonSerializerOptions on the base OfflineDbContext class. Maybe default the settings to the currentsettings to avoid breaking changes. |
Let me look at this more deeply on Friday (when I get a chance to work on this project). If it’s that simple, it should be easy to expose. However, what would be ideal is a project that “works when no Native AOT, but doesn’t work when Native AOT” is supplied as a cli able GitHub repository with instructions on how to adjust the project for Native AOT. Basically, I’m not a MAUI expert, so I can do the minimum needed to get something working. |
Hi @adrianhall unfortunately for Maui native Aot can't be disabled for iOS and catalyst https://learn.microsoft.com/en-us/dotnet/maui/macios/interpreter?view=net-maui-8.0 It's thrown up a few issues to say the least, even with efcore you have to use compiled models... which is a requirement for Aot. UseModel is a configuration option for the DbContextBuilder The ef tools don't run against a Maui project so you need segregate projects to run the tools. Which you need to generate the compiled modules and migrations etc. I'll document all the sticking points but I think at the moment it hasn't been an easy move from azure mobile apps. And this issue would block a release targeting iOS for anyone using Maui looking to migrate. I'll update the sample project in my fork for reference |
Hi @adrianhall just looking at this and mstest now supports testing with native Aot https://devblogs.microsoft.com/dotnet/testing-your-native-aot-dotnet-apps/ Will this support what your looking for in terms of a cli able application or were you thinking more of a console app that would work not Aot and then fail with Aot? Cheers Rich |
Not sure right now :-) I am thinking of working on the MAUI Todo app and turning on Native AOT. Not sure if that will work for Android, but if it does, it should be enough to show off the problem and I can go from there. |
FWIW, I believe that this is relatively easy.
That;s pretty much it. Then, in your code, you would do something like: DatasyncSerializer.JsonSerializerOptions.<whatever your changes are> It may also be worth my while to specify this in the EF Core extensions I have. Right now, I copy the reference to the JsonSerializerOptions from the DatasyncSerializer to the DbContext when it starts up; thus setting the DatasyncSerializer is the right thing to do. If you want to test it, it’s a 2 line change to the library. I can put it in a branch for you to test (probably tonight - assuming I can get my desktop back online tonight - it fried its brain this week and I had to replace the SSD) |
Will the crashlytics logs help? Also you can set a project property to emulate the Aot behaviour for the serializer it won't affect the DbContext though.
|
I feel your pain, spilt a can of Pepsi on my surface keyboard Monday evening... this week has been interesting. |
Hi @adrianhall I've updated the samples in a branch in my repository Updated Samples It seems there is Native AoT and Native AoT. The async extensions for EF Core use MakeGenericMethod under the hood so they fail on native AoT with the console app. So this method fails.
For Android there's a hard fail if reflection based serialization is enabled when syncing and if your not using a compiled model for the context. It doesn't seem to care about anything else. For iOS it has the same hard fails as Android but complains about dynamic code generation in EFCore. Enabling the MTouchInterpreter seems to get over this. The amendments in the samples and exposing the serializer options get passed the hard fails. |
I've just checked in a change that I think will fix this issue by allowing you to set the DatasyncSerializer.JsonSerializerOptions. Explicitly:
So, my belief is that if you do something like this: JsonSerializerOptions options = DatasyncSerializer.JsonSerializerOptions();
// TODO: Set the TypeInfo
DatasyncSerializer.JsonSerializerOptions = options Before you make any call into Datasync, it should do the right thing. @richard-einfinity Can you validate that this fixes your problem? I don't see what you did with the updated samples; perhaps doing a PR to update the samples will help us review them and see what is going on? |
@adrianhall Hi Adrian. I made a similar change in my fork and yes it worked for the serializer issue. There was a few more changes that had to be made for compiling in release mode. EF has to use compiled models. Compiled Models The tools won't work against the Maui project so they need to be put into a separate library project targeting net8. You need a dummy start up project then to reference the library project when running the tools. Doesn't look like Native AoT support is coming to EF 8 there seem to be some enhancements in EF9 . As i mentioned in a previous comment it seems we have difference flavours of Native AoT on the different platforms. Android, Apple and Windows all seem to have different outcomes. The changes I have made gets everything running again for my project and in the samples, in particular on iOS where it was blocking. I think the wider issue of AoT support will need to be parked probably until net / EF 10 in line with the support policy of the project. Hopefully there will be more support in the underlying frameworks then. |
Shifting the rest of the work to 9.0.0 release. |
Our Maui based solution is using Native Aot and reflection based serializarion is disabled for Aot projects.
I think this would mean exposing the serialization settings for the offline entities and / or providing a registration mechanism for providing JsonTypeInfo for the entities.
Happy to look into and propose possible solutions if it's not already on the radar.
The text was updated successfully, but these errors were encountered: