Skip to content

Support native AoT #132

Open
Open
@Odonno

Description

@Odonno

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions