Skip to content

Add support for Native AOT #1334

@epaulsen

Description

@epaulsen

The problem

Hi guys, first off I want to thank you for a fantastic project.
I am writing a netdaemon app, but due to constrained disk space in the runtime environment I am planning to use it I would like to minimize the app payload size.
The best option for me would be to deploy this using Native AOT and self-contained deployment, so it is just a file that I copy over to the device which will be running my app.

But alas, the app does not work, because NetDaemon uses reflection-based serialization with http requests and websockets, which is a no-go when running under Native AOT.

The proposed solution

I've played around a bit with the NetDaemon code, and it is possible to add a source-generated serialization context for the project, which allows for serialization to work under native AOT.

The downside with source-generated serialization context is that whenever a new type that needs serialization/deserialization is added to the project, it needs also to be added to the serializationcontext. I've done this for work before, and what we ended up with there was to write a unit test that checks that all classes in a namespace can be serialized and deserialized. With that you have a safe-guard when refactoring.

If this is something that you think is a good idea I am more than happy to submit a pullrequest for it.

The alternatives

The alternative for me would be to fork this repo, copy parts of it, or rewrite it myself.
But generally speaking, Native AOT means less memory footprint, smaller image size and faster startup time.
In an IOT setting, where all three are constrained, this means a huge improvement.

Additional context

Source Generation for json serialization:
https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/source-generation

Native AOT publishing:
https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions