Search code examples
c#.netmauimaui-community-toolkit

System.InvalidCastException: Arg_InvalidCastException: System.Collections.Frozen.SmallFrozenSet


When running my .NET MAUI app after compiling it in Release configuration, the following exception is thrown when calling Toast.Show():

System.InvalidCastException: Arg_InvalidCastException
   at System.Collections.Frozen.SmallFrozenSet`1.GSW[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]].Store(FrozenSet`1 )
   at System.Collections.Frozen.FrozenSetInternalBase`2[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065],[System.Collections.Frozen.SmallFrozenSet`1.GSW[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]], System.Collections.Immutable, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a]]..ctor(IEqualityComparer`1 )
   at System.Collections.Frozen.SmallFrozenSet`1[[UIKit.UIView, Microsoft.iOS, Version=17.2.0.0, Culture=neutral, PublicKeyToken=84e04ff9cfb79065]]..ctor(HashSet`1 )
   at System.Collections.Frozen.FrozenSet.CreateFromSet[UIView](HashSet`1 )
   at System.Collections.Frozen.FrozenSet.ToFrozenSet[UIView](IEnumerable`1 , IEqualityComparer`1 )
   at CommunityToolkit.Maui.Core.Views.AlertView.get_Children()
   at CommunityToolkit.Maui.Core.Views.AlertView.Initialize()
   at CommunityToolkit.Maui.Core.Views.Alert.Show()
   at CommunityToolkit.Maui.Alerts.Toast.ShowPlatform(CancellationToken token)
   at CommunityToolkit.Maui.Alerts.Toast.Show(CancellationToken token)
   at KinesiaOne.ViewModels.SettingsPageViewModel.VersionTapped()

This error does not occur when running the app compiled in Debug configuration.

Reproduction Steps

https://github.com/vouksh/CommunityToastBug

  1. Clone the repository
  2. Set the Bundle ID to one that you have a Distribution certificate for
  3. Build as Release and push to a device
  4. Press the "Click me" button and see the exception and that there is no toast displayed

Solution

  • Explanation

    This is caused by a bug in the .NET Runtime, across the Mono interpreter and AOT compilation.

    The .NET MAUI docs recommend adding the following code the csproj to AOT compile all assemblies, while still allowing the interpreter to perform dynamic code generation:

    https://learn.microsoft.com/dotnet/maui/macios/interpreter#enable-the-interpreter

    <PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
        <MtouchInterpreter>-all</MtouchInterpreter>
    </PropertyGroup>
    

    However, either the Mono interpreter or the AOT compiler fails to include FrozenCollection, which is being used by the .NET MAUI Community Toolkit, shown in the stack trace.

    Fix

    To resolve the problem, add System.Collections.Immutable to the <MtouchInterpreter> tag:

    <PropertyGroup Condition="$(TargetFramework.Contains('-ios')) and '$(Configuration)' == 'Release'">
        <MtouchInterpreter>-all,System.Collections.Immutable</MtouchInterpreter>
    </PropertyGroup>