Skip to content

CoreSync.Http.Client fails under NativeAOT: reflection-based System.Text.Json and MessagePack Typeless/contractless resolvers #22

Open

Description

@davidortinau

We sync a .NET 11 iOS app (NativeAOT, PublishAot=true) through CoreSync.Http.Client 0.1.129 + CoreSync.Sqlite 0.1.129. Under CoreCLR it works; under NativeAOT the first sync fails after the local GetChanges with SynchronizationException wrapping NotSupportedException:

'System.Text.Json.Serialization.Converters.DictionaryOfTKeyTValueConverter`3[
  System.Collections.Generic.Dictionary`2[System.String,System.Int32],System.String,System.Int32]'
is missing native code or metadata.

Cause: SyncProviderHttpClient uses PostAsJsonAsync / ReadFromJsonAsync<T> without a JsonTypeInfo, so System.Text.Json builds converters by reflection (BulkSyncChangeSet.ChangesByTable is Dictionary<string, int>). NativeAOT has no code for value-type generic instantiations it did not see at compile time.

We worked around it in the app by declaring a source-generated JsonSerializerContext for BulkSyncChangeSet, BulkChangeSetUploadItem, SyncAnchor, SyncVersion and SyncItem[] that is never used, only so the compiler emits those instantiations. With that, a full download (143 rows) succeeded under NativeAOT.

The second risk is CoreSyncMessagePackSerializer.Options = TypelessContractlessStandardResolver.Options: MessagePack's dynamic resolvers generate IL at run time (not available under NativeAOT), and Typeless resolves types by name. Our runs used the JSON path, so we have not hit it yet; we expect bulk upload/download through MessagePack to fail the same way.

Suggestions

  1. Accept a JsonSerializerOptions/IJsonTypeInfoResolver in SyncProviderHttpClientOptions (or ship a source-generated CoreSyncJsonContext for the HTTP DTOs) and pass JsonTypeInfo to the *AsJsonAsync calls.
  2. Offer an AOT-safe MessagePack configuration (source-generated formatters via MessagePack v3's analyzer/generator, or a non-Typeless contract for SyncItem values), keeping wire compatibility with existing servers and clients.
  3. Mark the assemblies IsAotCompatible once the warnings are gone.

Versions: CoreSync.Http.Client 0.1.129, CoreSync.Sqlite 0.1.129, MessagePack 2.5.187, .NET SDK 11.0.100-rc.2, iOS simulator NativeAOT.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions