RuntimeInstanceNotAllowed Error Initializing XmlSerializer on Android Release Build in .NET 9 #109724
Open
Description
Description
App fails with an exception in a release build of .NET 9 (.NET 8 and debug builds of .NET 9 work fine).
It looks like the fault happened because of a call on new XmlSerializer(typeof(Meal))
(deferring that call defers the fault).
The abbreviated stack trace looks like this:
System.ArgumentException: RuntimeInstanceNotAllowed
?, in object DefaultValueAttribute.get_Value()
?, in new XmlAttributes(ICustomAttributeProvider)
?, in XmlAttributes XmlReflectionImporter.GetAttributes(MemberInfo)
?, in bool XmlReflectionImporter.InitializeStructMembers(StructMapping, StructModel, bool, string, RecursionLimiter)
?, in StructMapping XmlReflectionImporter.ImportStructLikeMapping(StructModel, string, bool, XmlAttributes, RecursionLimiter)
...
(1 additional frame(s) were not displayed)
System.InvalidOperationException: XmlTypeReflectionError, DivisiBill.Models.Meal
?, in XmlSerializer Meal.get_MealSerializer()
?, in void Meal.SaveToStream(Stream streamParameter)
?, in void Meal.SaveToApp()
?, in void Meal.OverwriteCurrent()
?, in async Task Meal.BecomeCurrentMealAsync()
...
(12 additional frame(s) were not displayed)
Steps to Reproduce
- Clone the repository, pull the
class-constructor-fault
branch. - Start VS2022 and select the DivisiBill project and a release build.
- At the VS developer PowerShell prompt enter
dotnet restore
- Run the app without debugging on an Android Emulator or physical device (ctrl+F5). You should see:
5.Then
6.Press the left arrow to see: - Click "While Using the App" and see:
- At this point the app will fault.
If it were working correctlyyou would see:
Then
Link to public reproduction project repository
https://github.com/david-maw/DivisiBill.git
Version with bug
9.0.0-rc.2.24503.2
Is this a regression from previous behavior?
Yes, this used to work in .NET MAUI
Last version that worked well
8.0.93 SR9.3
Affected platforms
Android
Affected platform versions
Android 14
Did you find any workaround?
No response