Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at

  • Andrii 4 posts 105 karma points
    Dec 17, 2019 @ 10:45

    DI fails to resolve IService<T>

    If one service is registered in composer as IService<Foo> with implementation as FooService - DI works nice, injecting IService<Foo> as expected.

    But when registered more than one service as example: IService<Foo>, IService<Bee> - DI cannot resolve any of IService<T> anymore:

    composition.Register(typeof(IService<Foo>), typeof(FooService), Lifetime.Scope); 
            composition.Register(typeof(IService<Bee>), typeof(BeeService), Lifetime.Scope);

    Produces exception at runtime "Unresolved dependency"

    Similar issue is related to multiple implementations of simple service IService:

    composition.Register(typeof(IService), typeof(FooService), Lifetime.Scope);
                composition.Register(typeof(IService), typeof(BeeService), Lifetime.Scope);

    With getting dependency like this:

    Current.Factory.GetAllInstances<IService>().FirstOrDefault(x => x.ServiceType == ServiceType.Foo);

    Produces exception "Unable to resolve type: System.Collections.Generic.IEnumerable`1[MyApp.Services.IService]", but works just fine when only 1 service registered.

    Also some general issue with generic .RegisterFor<IService,Service>() - it doesnt work at all, only .Register with type definition works, as in examples above.

  • Lars-Erik Aabech 349 posts 1100 karma points MVP 7x c-trib
    Dec 18, 2019 @ 13:18
    Lars-Erik Aabech

    [Reposting same answer as on GitHub issue]

    This works differently in all DI containers. Some pick the first registered, some pick the last, some say "dunno,, cuz u have more than 1". I don't remember OTOH how this was solved in Umbraco, but I don't think it's supported or trustworthy via Composition. As long as something works differently in containers, it should be "unsupported" in Umbraco's abstractions to avoid compatibility issues.

    I recommend either registering a custom factory method looking up concrete registrations, or registering your generic services directly with lightinject.
    You can get the concrete container from:


    Unless you're building a package, in which case you should not do stuff Umbraco's DI abstraction doesn't support.

Please Sign in or register to post replies

Write your reply to: