AOT not support for DataContractSerializer for serialize and deserialize #30079
Replies: 2 comments 1 reply
-
Please share the way this fails and how did you enable AOT. |
Beta Was this translation helpful? Give feedback.
-
Hi Everyone, We are encountering an issue when enabling AOT. The serializer.WriteObject() method causes the app to crash in release mode on both Android and Mac platforms—even when AOT is not enabled at the sample level. We've incorporated serializer-related code changes in the source, as it's a dependent package for the sample. However, we were able to resolve the issue by adding the Preserve attribute alongside the DataContract attributes in the relevant classes. We are still unsure whether AOT is officially supported on the Android platform. Could you please clarify? I use below codes to enable AOT in project, <PropertyGroup Condition="'$(TargetFramework)' == 'net9.0' or '$(TargetFramework)' == 'net9.0-android' or '$(TargetFramework)' == 'net9.0-ios' or '$(TargetFramework)' == 'net9.0-maccatalyst' or '$(TargetFramework)' == 'net9.0-windows10.0.19041.0'">
<IsAotCompatible>true</IsAotCompatible>
</PropertyGroup> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
We have requirement of serializing and deserialize the custom class using the System.Runtime.Serialization.DataContractSerializer with the help of following code in the source code.
Serialize code snippet
annotationsData.Annotations?.Add(new AnnotationDetails { Name = "Test", Age = 30 });
var knownTypes = new List
{
typeof(AnnotationsData),
typeof(AnnotationDetails),
typeof(List),
typeof(string),
typeof(int)
};
DataContractSerializer serializer = new DataContractSerializer(typeof(AnnotationsData), knownTypes);
if (stream != null)
{
serializer.WriteObject(stream, annotationsData);
}
Deserialize code snippet
object? data = null;
var knownTypes = new List
{
typeof(AnnotationsData),
typeof(AnnotationDetails),
typeof(List),
typeof(string),
typeof(int)
};
DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(AnnotationsData), knownTypes);
if (stream != null && stream.Length > 0)
{
stream.Position = 0;
data = dataContractSerializer.ReadObject(stream);
}
The reported scenario occurs will serialize the class which contains the List when the AOT enable and publish. We have attached the replicate sample in the attachment. Is possible to serialize and deserialize without any crash?
MauiApp2.zip
Beta Was this translation helpful? Give feedback.
All reactions