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

NativeAot compatibility #56

Open
tipa opened this issue Jan 28, 2024 · 2 comments
Open

NativeAot compatibility #56

tipa opened this issue Jan 28, 2024 · 2 comments

Comments

@tipa
Copy link

tipa commented Jan 28, 2024

Due to the use of System.Xml.Serialization, this library isn't NativeAot-compatible.

using Geo.Gps.Serialization;

try
{
    var data = File.ReadAllBytes("path_to_gpx");
    using var stream = new MemoryStream(data);
    var streamWrapper = new StreamWrapper(stream);
    var gpsData = new Gpx10Serializer().CanDeSerialize(streamWrapper) ?
        new Gpx10Serializer().DeSerialize(streamWrapper) :
        new Gpx11Serializer().DeSerialize(streamWrapper);
}
catch (Exception e)
{
    Console.WriteLine(e);
}

dotnet publish
.\geo_nativeaot\bin\Release\net8.0\win-x64\publish\geo_nativeaot.exe

GpxFile cannot be serialized because it does not have a parameterless constructor.
   at System.Xml.Serialization.TypeDesc.CheckSupported() + 0x42
   at System.Xml.Serialization.TypeScope.GetTypeDesc(Type, MemberInfo, Boolean, Boolean) + 0xbd
   at System.Xml.Serialization.ModelScope.GetTypeModel(Type, Boolean) + 0x5a
   at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(Type, XmlRootAttribute, String) + 0x50
   at System.Xml.Serialization.XmlSerializer.GetMapping() + 0x4f
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) + 0x98
   --- End of inner exception stack trace ---
   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) + 0x209
   at Geo.Gps.Serialization.Xml.GpsXmlDeSerializer`1.DeSerialize(StreamWrapper) + 0xc7

When excluding the Geo assembly from trimming, another error surfaces:

<ItemGroup>
    <TrimmerRootAssembly Include="Geo" />
</ItemGroup>
System.InvalidOperationException: There is an error in XML document (3, 3).
 ---> System.NotSupportedException: 'System.Xml.Serialization.ReflectionXmlSerializationReaderHelper.GetSetMemberValueDelegateWithType[Geo.Gps.Serialization.Xml.Gpx.Gpx11.GpxMetadata,System.DateTime](System.Reflection.MemberInfo)' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
   at System.Reflection.Runtime.MethodInfos.RuntimeNamedMethodInfo`1.GetUncachedMethodInvoker(RuntimeTypeInfo[], MemberInfo) + 0x2f
...

Demo project: geo_nativeaot.zip

@sibartlett
Copy link
Owner

Hi @tipa,

Thank you for raising the issue… so that I can help better, what functionality do you require from Geo?

I think there’s either two paths I could take:

  • Find an alternative to the Serialization dependency, or
  • Extract the functionality that requires serialization to a sibling package… then the core library could be Native AOT compatible.

@tipa
Copy link
Author

tipa commented Jan 28, 2024

Hi @sibartlett,

thanks for your swift response.

Basically, my app reads gpx files, iterates over the Routes, Tracks & Waypoints properties of GpsData, collects all the "Coordinates" and displays them as paths and pins on a map view. I am not using the serialization parts of this library in my project.

I think System.Xml.Serialization is incompatible with NativeAot as it uses methods (like MethodInfo.MakeGenericMethod()) that use reflection.
In another app of mine where I used System.Xml.Serialization I was able to work around this limitation by rewriting the deserialization code using System.Xml.Linq. I don't know if this is an option for this library and how much work this would involve.

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