Skip to content
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 #132

Open
Odonno opened this issue Apr 14, 2024 · 2 comments
Open

Support native AoT #132

Odonno opened this issue Apr 14, 2024 · 2 comments

Comments

@Odonno
Copy link
Contributor

Odonno commented Apr 14, 2024

When trying to use this library in a native AoT context, I get the following error:

System.NotSupportedException: 'Dahomey.Cbor.Serialization.Converters.ObjectConverter`1[]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.General.TypeUnifier.WithVerifiedTypeHandle(RuntimeConstructedGenericTypeInfo, RuntimeTypeInfo[]) + 0x75
   at Dahomey.Cbor.Serialization.Converters.Providers.ObjectConverterProvider.GetConverter(Type, CborOptions) + 0xe2
   at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext() + 0x74
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1, Func`2, Boolean&) + 0x7c
   at Dahomey.Cbor.Serialization.Converters.CborConverterRegistry.CreateConverter(Type type) + 0xbd
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey, Func`2) + 0xa4
   at Dahomey.Cbor.Serialization.Converters.CborConverterRegistry.Lookup[T]() + 0x25
   at Dahomey.Cbor.Cbor.Serialize[T](T, IBufferWriter`1&, CborOptions) + 0x58
   at Dahomey.Cbor.Cbor.SerializeAsync[T](T, Stream, CborOptions, CancellationToken) + 0x80

One solution would be to register types via CborSerializable with a CborContext, the same way it is done for System.Text.Json. See the example for reference:

[JsonSourceGenerationOptions(
    AllowTrailingCommas = true,
    NumberHandling = JsonNumberHandling.AllowReadingFromString
        | JsonNumberHandling.AllowNamedFloatingPointLiterals,
    PropertyNameCaseInsensitive = true,
    ReadCommentHandling = JsonCommentHandling.Skip
)]
[JsonSerializable(typeof(Product))]
internal partial class SurrealDbWsJsonSerializerContext : JsonSerializerContext;
@mcatanzariti
Copy link
Member

Hi @Odonno,

Dahomey.Cbor is heavily based on reflexion that kind of generates code at runtime. For instance Expression trees are used at some places.
It is absolutely not compatible with AOT.

AFAIK, System.Text.Json is using Source generators to generate code at compile time instead of reflexion at runtime. By this way, it is compatible with AOT.

It would require a full rewrite of multiple core parts of Dahomey.Cbor, especially the ObjectConverter class.

I'm not ready to follow this path at the moment...

Cheers,

Michaël

@Odonno
Copy link
Contributor Author

Odonno commented Apr 15, 2024

Hello Michaël,

Indeed, System.Text.Json is using Source Generators to avoid the use of Reflection. This has to be the inspiration for this library, having attributes like CborSourceGenerationOptions and CborSerializable to generate the necessary code.

Like you said, this can require a lot of effort to support it, especially if you rely heavily on Reflection. That can be a long term goal to make it fully compatible.

Thank you for your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants