Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to version 8.0.2 failed to resolve IMvxMessenger on start #4347

Open
1 task done
nicolgit opened this issue Dec 18, 2021 · 16 comments
Open
1 task done

Upgrade to version 8.0.2 failed to resolve IMvxMessenger on start #4347

nicolgit opened this issue Dec 18, 2021 · 16 comments

Comments

@nicolgit
Copy link

🔙 Regression

After upgrading from 7.x to version 8.0.2 the app throws a Failed to resolve first ViewModel

Old (and correct) behavior

App starts up normally.

Current behavior

on start throw the following exception

{MvvmCross.Exceptions.MvxException}	MvvmCross.Exceptions.MvxException
Exception: "Failed to construct and initialize ViewModel for type CaledosLab.Runner.Commons.ViewModels.MainPageViewModel from locator MvxDefaultViewModelLocator - check InnerException for more information"

  at MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.ViewModels.MvxViewModelRequest request, MvvmCross.ViewModels.IMvxBundle savedState, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs navigationArgs) [0x00044] in /_/MvvmCross/ViewModels/MvxViewModelLoader.cs:74 
  at MvvmCross.Navigation.MvxNavigationService.Navigate (System.Type viewModelType, MvvmCross.ViewModels.IMvxBundle presentationBundle, System.Threading.CancellationToken cancellationToken) [0x00019] in /_/MvvmCross/Navigation/MvxNavigationService.cs:454 
  at MvvmCross.Navigation.MvxNavigationService.Navigate[TViewModel] (MvvmCross.ViewModels.IMvxBundle presentationBundle, System.Threading.CancellationToken cancellationToken) [0x00000] in /_/MvvmCross/Navigation/MvxNavigationService.cs:501 
  at CaledosLab.Runner.Commons.ViewModels.CaledosAppStart.NavigateToFirstViewModel (System.Object hint) [0x00018] in C:\Caledos\code\Runner\CaledosLab.Runner.Commons.ViewModels\App.cs:53 

InnerException	{MvvmCross.Exceptions.MvxException}	MvvmCross.Exceptions.MvxException
Message "Problem creating viewModel of type MainPageViewModel"

  at MvvmCross.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.ViewModels.IMvxBundle parameterValues, MvvmCross.ViewModels.IMvxBundle savedState, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs navigationArgs) [0x00029] in /_/MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs:32 
  at MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.ViewModels.MvxViewModelRequest request, MvvmCross.ViewModels.IMvxBundle savedState, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs navigationArgs) [0x00031] in /_/MvvmCross/ViewModels/MvxViewModelLoader.cs:70 

InnerException	{MvvmCross.Exceptions.MvxIoCResolveException}	MvvmCross.Exceptions.MvxIoCResolveException
Message	"Problem creating viewModel of type MainPageViewModel"	string
  at MvvmCross.ViewModels.MvxDefaultViewModelLocator.Load (System.Type viewModelType, MvvmCross.ViewModels.IMvxBundle parameterValues, MvvmCross.ViewModels.IMvxBundle savedState, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs navigationArgs) [0x00029] in /_/MvvmCross/ViewModels/MvxDefaultViewModelLocator.cs:32 
  at MvvmCross.ViewModels.MvxViewModelLoader.LoadViewModel (MvvmCross.ViewModels.MvxViewModelRequest request, MvvmCross.ViewModels.IMvxBundle savedState, MvvmCross.Navigation.EventArguments.IMvxNavigateEventArgs navigationArgs) [0x00031] in /_/MvvmCross/ViewModels/MvxViewModelLoader.cs:70 

Reproduction steps

the exception raises here(***)

public class App : MvvmCross.ViewModels.MvxApplication
    {
        public override void Initialize()
        {
            CreatableTypes()
                .EndingWith("Service")
                .AsInterfaces()
                .RegisterAsLazySingleton();
            
            InitializeAppConfiguration();
            InitializeAppText();

            RegisterCustomAppStart<CaledosAppStart>();
        }

        private void InitializeAppConfiguration()
        {
            var languageManager = new LanguageManager();
            Mvx.IoCProvider.RegisterSingleton<ILanguageManager>(languageManager);
        }

        private void InitializeAppText()
        {
            TextLoader.Instance.LoadJsonFromResource("Shared.json", "Shared");                        
        }
    }

public class CaledosAppStart : MvxAppStart
    {
        public CaledosAppStart(IMvxApplication application, IMvxNavigationService navigationService) : base(application, navigationService)
        {
        }

        protected override async Task NavigateToFirstViewModel(object hint = null)
        {
            try
            {
                await NavigationService.Navigate<MainPageViewModel>();
            }
            catch (System.Exception e)
            {

                throw; (***)
            }
            
        }
    }

Configuration

Version: 8.0.2

Platform:

  • 🤖 Android
@nicolgit nicolgit changed the title Upgrade to version 8.0.2 failed to resolve First ViewModel Upgrade to version 8.0.2 failed to resolve First ViewModel on ANDROID Dec 18, 2021
@Cheesebaron Cheesebaron changed the title Upgrade to version 8.0.2 failed to resolve First ViewModel on ANDROID Upgrade to version 8.0.2 failed to resolve First ViewModel Dec 18, 2021
@Cheesebaron
Copy link
Member

Comment out stuff in your MainPageViewModel constructor untill you find out which dependency fails resolving.

@nicolgit
Copy link
Author

nicolgit commented Dec 18, 2021

@Cheesebaron thank you for the hint, the constructor crashes on the following row:

token = Mvx.IoCProvider.Resolve<IMvxMessenger>().Subscribe<UserMessage>(OnUserMessage);

any idea?

@Cheesebaron
Copy link
Member

I think I've seen other issues with regards to MvxMessenger breaking in other issues, where they just downgraded that package to 8.0.1.

@nicolgit
Copy link
Author

downgraded MvxMessenger to 8.0.1 but the issue is the same :-(

@nicolgit nicolgit changed the title Upgrade to version 8.0.2 failed to resolve First ViewModel Upgrade to version 8.0.2 failed to resolve IMvxMessenger on start Dec 20, 2021
@nicolgit
Copy link
Author

Any Idea to unlock the situation? do you think it is better to rollback to 7 and wait for a future MVVM release?

@tvone-timmoore
Copy link

tvone-timmoore commented Apr 4, 2022

With Mvx 8.0.2, WPF, .NET 5.

I'm having a similar problem when trying to publish a message from code called from MvxAppStart.NavigateToFirstViewModel() and the MvxMessenger is getting stuck waiting for the UI Thread.

I'm processing the command line args to load a file and navigate to the correct viewmodel, the same code works when selecting a load button after the UI shows.

This previously worked with Mvx 7 and lower.

[Edit] Will raise a new bug as it's not quite the same.

@entdark
Copy link
Contributor

entdark commented Apr 4, 2022

I'm having a similar problem when trying to publish a message from code called from MvxAppStart.NavigateToFirstViewModel() and the MvxMessenger is getting stuck waiting for the UI Thread.

Could it be related to #4364?

@tvone-timmoore
Copy link

I'm having a similar problem when trying to publish a message from code called from MvxAppStart.NavigateToFirstViewModel() and the MvxMessenger is getting stuck waiting for the UI Thread.

Could it be related to #4364?

Thanks @entdark, yes is could be related that #4364, I've added my comment to that bug.

I've just started to have a look at MvvmCross 8 code to see why it is blocking in 8 but not 7.

@ivmirx
Copy link
Contributor

ivmirx commented May 2, 2022

Same here, but it happens only on iOS and only in release builds, I was able to catch it only by catching an exception in static void Main(string[] args) and logging it via the WiFi console in Xcode:

MvvmCross.Exceptions.MvxIoCResolveException: Failed to resolve type MvvmCross.Plugin.Messenger.IMvxMessenger
  at MvvmCross.IoC.MvxIoCContainer.Resolve (System.Type type) <0x105051f74 + 0x00148> in …
  at MvvmCross.IoC.MvxIoCContainer.Resolve[T] () <0x105051f10 + 0x0002f> in …
  at MvvmCross.IoC.MvxIoCProvider.Resolve[T] () <0x10505614c + 0x00033> in …
  at MyApp.Core.State..ctor (MyApp.Core.IDatabaseService databaseService, Plugin.StoreReview.Abstractions.IStoreReview storeReview) <0x105903658 + 0x0024f> in …
  at MyApp.Core.App.Initialize () <0x1058f8e4c + 0x00093> in …
  at MvvmCross.Core.MvxSetup.InitializeApp (MvvmCross.Plugin.IMvxPluginMa<…>

Looks like a race condition that does not happen in slower debugging-enabled builds. Linker settings do not affect the crash.

@ivmirx
Copy link
Contributor

ivmirx commented May 2, 2022

Solved the problem by adding this to App.Initialize() before registering any of my singletons and calling RegisterAppStart<…>():

var pluginManager = Mvx.IoCProvider.Resolve<IMvxPluginManager>();
pluginManager.EnsurePluginLoaded<MvvmCross.Plugin.Messenger.Plugin>(true);

@entdark
Copy link
Contributor

entdark commented May 5, 2022

Could that be related: AppRopio/AppRopio.Mobile@2e19235?
I fixed it by using ToList() that forced to iterate through all the items in IEnumerable. I did not pay much attention to that back then but now looking to the source I see this:

.AsParallel()

And since it is using AsParallel() it is not waiting for result when iterating through all the plugins when loading them. So the app initialization happens faster than all the plugins get loaded.

@entdark
Copy link
Contributor

entdark commented May 5, 2022

By the way, there is no point in AsParallel() if it's executed in foreach. It has to be ForAll(item => {}) extension for the parallel query.

@entdark
Copy link
Contributor

entdark commented May 5, 2022

My hypothesis was wrong about parallel query (or maybe not - I will get to that topic later). But I found out another thing: that method returns false for type MvvmCross.Plugin.Messenger.Plugin:

if (TypeContainsPluginAttribute(type))

Even worse: if to try to load all possible attributes manually it still finds 0.
So MvxMessenger never gets loaded.
If that condition is commented out then things go fine.
I am reminding that it happens in Release builds only.

@entdark
Copy link
Contributor

entdark commented May 17, 2022

Solved the problem by adding this to App.Initialize() before registering any of my singletons and calling RegisterAppStart<…>():

Seems like the proper place would be overriden App.LoadPlugins(IMvxPluginManager pluginManager):

public virtual void LoadPlugins(IMvxPluginManager pluginManager)

@entdark
Copy link
Contributor

entdark commented May 17, 2022

@ivmirx

@ValonK
Copy link

ValonK commented Nov 21, 2023

I tried the workaround mentioned earlier, but unfortunately, it didnt work for me. Additionally, I've completely removed MvxMessenger from the project. However, I'm still encountering the issue when running the app in Release Mode on iOS (stuck on splash screen then crashes), while Android works correctly.

We haven't transitioned our app to MAUI yet, so we're currently stuck using Xamarin.Forms with MvvmCross 8.0.2. (7.1.2) works correctly.

The iOS logs haven't provided any useful insights. Has anyone found a solution for this problem?

MvvmCross 8.0.2
MvvmCross.Forms 8.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

6 participants