0
votes

I'm using MvvmCross v6.2.1 in an iOS project and am getting the following exception: MvvmCross.Exceptions.MvxIoCResolveException has been thrown. Failed to resolve type Countr.Core.Services.IMediaManagerService. In an attempt to solve this I added some debugging code to App.cs

using MvvmCross.IoC;
using Countr.Core.ViewModels;
using Countr.Core.Services;
using MvvmCross;
using MvvmCross.ViewModels;
using System.Linq;

namespace Countr.Core
{
    public class App : MvxApplication
    {
        public override void Initialize()
        {
            CreatableTypes()
                .EndingWith("Service")
                .AsInterfaces()
                .RegisterAsLazySingleton();

            CreatableTypes()
                .EndingWith("Repository")
                .AsInterfaces()
                .RegisterAsLazySingleton();

         var x = new MediaManagerService();
         var y = this.CreatableTypes().FirstOrDefault(t => t.Name == "MediaManagerService");
         var z = Mvx.IoCProvider.Resolve<IMediaManagerService>();

            RegisterAppStart<ArticlesViewModel>();
        }
    }
}

The variables x and y are successfully created, i.e., there is no error creating the MediaManagerService and the MediaManagerService is listed in the CreatableTypes() collection. However the code blows up when initializing the variable z, i.e., when executing the Resolve method.

What am I doing wrong?

1
Did it works fine in old version? - Lucas Zhang - MSFT
Does Mvx.IoCProvider.GetSingleton<IMediaManagerService>() throws the same error? does the exception have an InnerException or it's just that? - fmaccaroni
Lucas, this is new code. No old version to test. - Tom Corrigan
fmaccaroni. GetSingleton() throws the same error. - Tom Corrigan

1 Answers

0
votes

Check if you have more than one MediaManagerService because it could be trying to construct another instance.

Instead of

var y = this.CreatableTypes().FirstOrDefault(t => t.Name == "MediaManagerService");

check

var ylist = this.CreatableTypes().Where(t => t.Name == "MediaManagerService").ToList();

If you still have just one occurrence of MediaManagerService try registering like

Mvx.IoCProvider.LazyConstructAndRegisterSingleton<IMediaManagerService, MediaManagerService>();

If you have more than one occurrence of the implementation then you can do the aforementioned solution for one occurrence after the registering of the CreatableTypes so that it overrides the previous registration or you can simple change the name of the interface/implementation so that the scan gives you a unique pair of Interface/Implementation to register.