Search code examples
c#wpfmvvm.net-5catel

Catel.IoC.TypeNotRegisteredException: Catel throws exception even though CanResolve returns true


I have a WPF Application with Catel.Core, Catel.MVVM, Catel.Analyzers and Catel.Fody installed.

When I start the application and try to resolve a Data Access Repository (ILigaMannschaftenZuordnungenRepository) the exception Catel.IoC.TypeNotRegisteredException gets thrown. When I call dependencyResolver.CanResolve<ILigaMannschaftenZuordnungenRepository>(); it returns true.

I made an extension method to register every service and repository in a single place.

public static IServiceLocator RegisterDataAccessRepositories(this IServiceLocator services, string dbCon)
{
                services.RegisterInstance(new SpielbetriebDbContext(dbCon));
                services.RegisterType<IDuelleRepository, DuelleRepository>();
                services.RegisterType<IDuellErgebnisseRepository, DuellErgebnisseRepository>();
                services.RegisterType<ILigaMannschaftenZuordnungenRepository, LigaMannschaftenZuordnungenRepository>(); // It is registered!
                services.RegisterType<IMitgliederRepository, MitgliederRepository>();
                services.RegisterType<IPartieDetailsRepository, PartieDetailsRepository>();
                services.RegisterType<IPartienRepository, PartienRepository>();
                services.RegisterType<IProtokollierungenRepository, ProtokollierungenRepository>();
                services.RegisterType<ISaetzeRepository, SaetzeRepository>();
                services
                    .RegisterType<ISpielerPartieErgebnisZuordnungenRepository,
                        SpielerPartieErgebnisZuordnungenRepository>();
                services.RegisterType<ISpielerRepository, SpielerRepository>();
                services.RegisterType<IVereineRepository, VereineRepository>();
    
                return services;
}

When I debug the application and check the registered types of the ServiceLocator it even shows me that ILigaMannschaftenZuordnungenRepository is registered (field _registeredTypes): Debugging servicelocator

The extensions get called in OnStartup. I even tried in the constructor of the App class.

var serviceLocator = this.GetServiceLocator();
serviceLocator.RegisterDataAccessRepositories(DataConfig.CurrentDbCon); // <--
serviceLocator.RegisterApplicationServices();

I tried to call it right away with the Resolve Method

var dep = this.GetDependencyResolver();
dep.Resolve<ILigaMannschaftenZuordnungenRepository>();
dep.CanResolve<ILigaMannschaftenZuordnungenRepository>(); // returns true

What did do I wrong?

Exception Message enter image description here


Solution

  • Okay, after debugging for quite some time and checking multiple things, I looked into my Data Access Repositories again. And I realized that my repository's implementation had an internal constructor instead of a public constructor. The cause was the base Repository, which had an internal constructor. After changing from internal to public the DI Container resolved the types correctly.

    If you are running into this problem, check if your types have public constructors.

    Bad:

    internal LigaMannschaftenZuordnungenRepository(SpielbetriebDbContext context) : base(context)
    {
    }
    

    Good:

    public LigaMannschaftenZuordnungenRepository(SpielbetriebDbContext context) : base(context)
    {
    }