Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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.
[Reposting same answer as on GitHub issue https://github.com/umbraco/Umbraco-CMS/issues/7347]
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.
is working on a reply...
Write your reply to:
Image will be uploaded when post is submitted