dotnet icon indicating copy to clipboard operation
dotnet copied to clipboard

[iOS] Application crashes on load after updating to the latest version of CommunityToolkit.Mvvm

Open npostma opened this issue 1 year ago • 9 comments

Describe the bug

Hi, after updating the nuGet packet i get the folling error when starting the application:

2024-08-31 09:52:53.920138+0200 <APPNAME>[54180:1568153] System.TypeLoadException: Could not set up parent class, due to: Invalid generic instantiation assembly:/Users/<USERNAME>/Library/Developer/CoreSimulator/Devices/A138E2DE-3DF0-4BC2-8889-BA1CBC699ED6/data/Containers/Bundle/Application/79E58D76-D3A6-4060-B4FA-4B109B01FB74/<APPNAME>.app/Microsoft.Maui.dll type:ViewHandler`2 member:(null)
   at CommunityToolkit.Maui.AppBuilderExtensions.<>c.<UseMauiCommunityToolkit>b__0_0(IMauiHandlersCollection h)
2024-08-31 09:52:53.920397+0200 <APPNAME>[54180:1568153]    at Microsoft.Maui.Hosting.HandlerMauiAppBuilderExtensions.HandlerRegistration.AddRegistration(IMauiHandlersCollection builder)
   at Microsoft.Maui.Hosting.Internal.MauiHandlersFactory.CreateHandlerCollection(IEnumerable`1 registrationActions)
2024-08-31 09:52:53.920607+0200 <APPNAME>[54180:1568153]    at Microsoft.Maui.Hosting.Internal.MauiHandlersFactory..ctor(IEnumerable`1 registrationActions)
   at Microsoft.Maui.Hosting.HandlerMauiAppBuilderExtensions.<>c.<ConfigureMauiHandlers>b__1_0(IServiceProvider sp)
2024-08-31 09:52:53.920812+0200 <APPNAME>[54180:1568153]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, RuntimeResolverContext context)
2024-08-31 09:52:53.921040+0200 <APPNAME>[54180:1568153]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSiteMain(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
2024-08-31 09:52:53.921275+0200 <APPNAME>[54180:1568153]    at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.RuntimeResolverContext, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[System.Object, System.Private.CoreLib, Version=8.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]].VisitCallSite(ServiceCallSite callSite, RuntimeResolverContext argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
2024-08-31 09:52:53.921452+0200 <APPNAME>[54180:1568153]    at Microsoft.Extensions.DependencyInjection.ServiceProvider.CreateServiceAccessor(ServiceIdentifier serviceIdentifier)
2024-08-31 09:52:53.921692+0200 <APPNAME>[54180:1568153]    at System.Collections.Concurrent.ConcurrentDictionary`2[[Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceIdentifier, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60],[Microsoft.Extensions.DependencyInjection.ServiceProvider.ServiceAccessor, Microsoft.Extensions.DependencyInjection, Version=8.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]].GetOrAdd(ServiceIdentifier key, Func`2 valueFactory)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
2024-08-31 09:52:53.923808+0200 <APPNAME>[54180:1568153]    at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)
2024-08-31 09:52:53.928044+0200 <APPNAME>[54180:1568153]    at Microsoft.Maui.MauiContext.WrappedServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
2024-08-31 09:52:53.930845+0200 <APPNAME>[54180:1568153]    at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[IMauiHandlersFactory](IServiceProvider provider)
   at Microsoft.Maui.MauiContext.<.ctor>b__2_0()
   at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ViaFactory(LazyThreadSafetyMode mode)
2024-08-31 09:52:53.932735+0200 <APPNAME>[54180:1568153]    at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
2024-08-31 09:52:53.934098+0200 <APPNAME>[54180:1568153]    at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].CreateValue()
   at System.Lazy`1[[Microsoft.Maui.IMauiHandlersFactory, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].get_Value()
2024-08-31 09:52:53.935420+0200 <APPNAME>[54180:1568153]    at Microsoft.Maui.MauiContext.get_Handlers()
   at Microsoft.Maui.Platform.ElementExtensions.SetHandler(INativeObject nativeElement, IElement element, IMauiContext context)
2024-08-31 09:52:53.936655+0200 <APPNAME>[54180:1568153]    at Microsoft.Maui.Platform.ElementExtensions.SetApplicationHandler(IUIApplicationDelegate platformApplication, IApplication application, IMauiContext context)
   at Microsoft.Maui.MauiUIApplicationDelegate.FinishedLaunching(UIApplication application, NSDictionary launchOptions)
2024-08-31 09:52:53.937739+0200 <APPNAME>[54180:1568153]    at ActiveID.AppDelegate.FinishedLaunching(UIApplication app, NSDictionary options) in <PATH_TO_APPLICATION>/<APPNAME>/<APPNAME>/Platforms/iOS/AppDelegate.cs:line 27
2024-08-31 09:52:53.938361+0200 <APPNAME>[54180:1568153]    at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
2024-08-31 09:52:53.938942+0200 <APPNAME>[54180:1568153]    at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at ActiveID.Program.Main(String[] args) in <PATH_TO_APPLICATION>/<APPNAME>/<APPNAME>/Platforms/iOS/Program.cs:line 14
Stopping iOS process...
iOS Process was stopped.

This occurs only on iOS, for release builds in testflight and debug builds, but not for any of my Android builds.

The way i initialize the toolkit:

public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder.UseMauiApp<App>()
.UseSkiaSharp()
.RegisterFirebaseServices()
.UseBarcodeReader()
.UseMauiCommunityToolkit();

 return builder.Build();
}

The way i register my WeakReferenceMessenger is:

/// <summary>
/// Subscribe listeners that listen to device specific events
/// </summary>
protected void SubscribeListeners()
{
    WeakReferenceMessenger.Default.Register<PushNotificationReceived>(this, (r, notificationReceived) =>
    {
        DisplayAlertAsync(r, notificationReceived.Value);
    });
}

Regression

It worked in version 8.2.0

Steps to reproduce

Not sure, i had 8.2.0 Of Mvvm installed. I updated the Toolkit and the .Mvvm package to the latest versions, and now the application wont start.

If you want to know specifics, let me know.

Expected behavior

That the iOS application does not crash after showing the splash screen.

Screenshots

No response

IDE and version

Other

IDE version

VS For mac and Visual Studio Code on mac.

Nuget packages

  • [ ] CommunityToolkit.Common
  • [ ] CommunityToolkit.Diagnostics
  • [ ] CommunityToolkit.HighPerformance
  • [X] CommunityToolkit.Mvvm (aka MVVM Toolkit)

Nuget package version(s)

8.3.0

Additional context

No response

Help us help you

Yes, but only if others can assist

npostma avatar Aug 31 '24 08:08 npostma

sorry but i am not installing anything from a random url .... 😆

[EDIT] Above was some spam bot .... i see the comment is removed now.

npostma avatar Aug 31 '24 08:08 npostma

Is this only happening on Windows by any chance? Can you try the workaround in https://github.com/CommunityToolkit/dotnet/issues/923#issuecomment-2314444048? I wonder if it's the same issue 🤔

Sergio0694 avatar Aug 31 '24 17:08 Sergio0694

100% reproducible crash for me when running app on Windows. I tried installing the latest WinRT.Runtime as:

NuGet\Install-Package Microsoft.Windows.CsWinRT -Version 2.1.1

but that just caused it to crash a bit later in the code.

went back to <PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.2" />

and all is well?

It crashed on this line in app.xaml.cs

protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

System.TypeInitializationException HResult=0x80131534 Message=The type initializer for '<Module>' threw an exception. Source=MauiDiag StackTrace: at MauiDiag.MauiProgram.CreateMauiApp() in G:\VS Projects\GrpcService\MauiDiag\MauiProgram.cs:line 237 at MauiDiag.WinUI.App.CreateMauiApp() in G:\VS Projects\GrpcService\MauiDiag\Platforms\Windows\App.xaml.cs:line 22 at Microsoft.Maui.MauiWinUIApplication.OnLaunched(LaunchActivatedEventArgs args) at Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml.IApplicationOverrides.OnLaunched(LaunchActivatedEventArgs args) at ABI.Microsoft.UI.Xaml.IApplicationOverrides.Do_Abi_OnLaunched_0(IntPtr thisPtr, IntPtr args)

This exception was originally thrown at this call stack: [External Code]

Inner Exception 1: FileNotFoundException: Could not load file or assembly 'WinRT.Runtime, Version=2.1.0.0, Culture=neutral, PublicKeyToken=99ea127f02d97709'. The system cannot find the file specified.

exquirentibus avatar Aug 31 '24 17:08 exquirentibus

@exquirentibus like I said in the other comment, can you try the workaround in https://github.com/CommunityToolkit/dotnet/issues/923#issuecomment-2314444048?

Sergio0694 avatar Aug 31 '24 17:08 Sergio0694

Is this only happening on Windows by any chance No, this is happening on a MAC (mini). Latest OS. In Visual Studio for MAC (E.O.L, since this month) and in Visual Studio Code on the mac. So i don't think the suggested workaround is going to work in my case.

npostma avatar Sep 02 '24 05:09 npostma

I got the same issue. My MAUI app stopped working when I upgraded to CommunityToolkit.Mvvm Ver. 8.3.0. I pinpointed the problem down to the code below.

`        WeakReferenceMessenger.Default.Register<CapturedQRData>(this, async (sender, oCapturedQRData) =>
        {
            // If we have a Guid, try creating a new registration
            if (!oCapturedQRData.QRGuid.Equals(Guid.Empty))
            {
                bool lError = !await NewRegistration(oCapturedQRData.QRGuid, oPClockPref.General.HardwareID.ToString());
            }
        });

When I remark the above code, it works. I reverted back to Ver. 8.2.2 and it works fine.

See also: https://stackoverflow.com/questions/78919502/cannot-use-weakreferencemessenger-inside-maui-windows

JP

JPScerri avatar Sep 02 '24 07:09 JPScerri

I reverted back to Ver. 8.2.2 and it works fine. Same for me. For now i am on 8.2.2

npostma avatar Sep 03 '24 17:09 npostma

I don't know if this is relevant, but the publish date on the 8.3.0 CommunityToolkit.Mvvm package is very wrong. Sunday, December 31, 1899 (12/31/1899)

scott-cote avatar Sep 04 '24 14:09 scott-cote

I suspect that it is on purpose to revoke the release of that version? Latest stable in the NuGet manager is 8.2.2.....

npostma avatar Sep 05 '24 06:09 npostma