Skip to content

RuntimeInstanceNotAllowed Error Initializing XmlSerializer on Android Release Build in .NET 9 #109724

Open
@david-maw

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

  1. Clone the repository, pull the class-constructor-fault branch.
  2. Start VS2022 and select the DivisiBill project and a release build.
  3. At the VS developer PowerShell prompt enter dotnet restore
  4. Run the app without debugging on an Android Emulator or physical device (ctrl+F5). You should see:Image
    5.ThenImage
    6.Press the left arrow to see:Image
  5. Click "While Using the App" and see:Image
  6. At this point the app will fault.

If it were working correctlyyou would see:Image
ThenImage

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

Relevant log output

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions