Uploaded image for project: 'Realm .NET SDK'
  1. Realm .NET SDK
  2. RNET-1024

[ NativeAot ] Improve support

      Problem

      Currently attempt to use NativeAot leads to "Unhandled Exception: System.InvalidOperationException: No properties in Item, has linker stripped it?" exception. Expectedly, adding Preserve attribute has no effect in this case.

      MCVE (RealmAot1)

      csproj:

      <Project Sdk="Microsoft.NET.Sdk">
      
      	<PropertyGroup>
      		<OutputType>Exe</OutputType>
      		<TargetFramework>net7.0</TargetFramework>
      		<ImplicitUsings>enable</ImplicitUsings>
      		<Nullable>enable</Nullable>
      		<PublishAot>true</PublishAot>
      	</PropertyGroup>
      
      	<ItemGroup>
      		<PackageReference Include="Realm" Version="11.3.0" />
      	</ItemGroup>
      </Project>
      

      cs:

      Unable to find source-code formatter for language: cs. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      using MongoDB.Bson;
      using Realms;
      
      var config = new RealmConfiguration("Realm1.realm")
      {
          Schema = new[]
          {
              typeof(Item)
          }
      };
      var realm = Realm.GetInstance(config);
      var id = new ObjectId("64bb9508e033d8cfa91e66d3");
      var item = realm.Find<Item>(id);
      if (item is null)
      {
          var testItem = new Item
          {
              Id = id,
              Name = "Do this thing",
              Status = "Open",
              Assignee = "Aimee"
          };
          realm.Write(() =>
          {
              item = realm.Add(testItem);
              Console.WriteLine($"[ Added ] {item.Name} : {item.Status} > {item.Assignee}");
          });
      }
      else
      {
          Console.WriteLine($"{item.Name} : {item.Status} > {item.Assignee}");
      }
      
      public partial class Item : IRealmObject
      {
          [PrimaryKey]
          [MapTo("_id")]
          public ObjectId Id { get; set; } = ObjectId.GenerateNewId();
          [MapTo("assignee")]
          public string? Assignee { get; set; }
          [MapTo("name")]
          public string? Name { get; set; }
          [MapTo("status")]
          public string? Status { get; set; }
      }
      

      Workaround: add the assembly to custom rd

      add to csproj

      	<ItemGroup>
      		<RdXmlFile Include="Custom.rd.xml" />
      	</ItemGroup>
      

      Custom.rd.xml

      <Directives>
      	<Application>
      		<Assembly Name="RealmAot1" Dynamic="Required All" />
      	</Application>
      </Directives>
      

      After that works good! But using rd is considered as bad practice, ideally it should just work.

      Also, generating native code step produces some warnings:

        Generating native code
      \realm\11.3.0\lib\net6.0\Realm.dll : warning IL2104: Assembly 'Realm' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
      \mongodb.bson\2.19.1\lib\netstandard2.1\MongoDB.Bson.dll : warning IL3053: Assembly 'MongoDB.Bson' produced AOT analysis warnings.
      \mongodb.bson\2.19.1\lib\netstandard2.1\MongoDB.Bson.dll : warning IL2104: Assembly 'MongoDB.Bson' produced trim warnings. For more information see https://aka.ms/dotnet-illink/libraries
      \runtime.win-x64.microsoft.dotnet.ilcompiler\7.0.8\framework\System.Linq.Expressions.dll : warning IL3053: Assembly 'System.Linq.Expressions' produced AOT analysis warnings.
      

      How important is this improvement for you?

      Would be a major improvement

      Feature would mainly be used with

      Local Database only

            Assignee:
            Unassigned Unassigned
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated: