CommunityToolkit / dotnet

.NET Community Toolkit is a collection of helpers and APIs that work for all .NET developers and are agnostic of any specific UI platform. The toolkit is maintained and published by Microsoft, and part of the .NET Foundation.
https://docs.microsoft.com/dotnet/communitytoolkit/?WT.mc_id=dotnet-0000-bramin
Other
3.06k stars 299 forks source link

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

Open npostma opened 2 months ago

npostma commented 2 months ago

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

Nuget package version(s)

8.3.0

Additional context

No response

Help us help you

Yes, but only if others can assist

npostma commented 2 months ago

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

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

Sergio0694 commented 2 months ago

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 🤔

exquirentibus commented 2 months ago

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 '' 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.

Sergio0694 commented 2 months ago

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

npostma commented 2 months ago

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.

JPScerri commented 2 months ago

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

npostma commented 2 months ago

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

scott-cote commented 2 months ago

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)

npostma commented 2 months ago

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