dahomey-technologies/Dahomey.Cbor

Support native AoT

Opened this issue · 2 comments

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;

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

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.