LightInject icon indicating copy to clipboard operation
LightInject copied to clipboard

Keyed Services Support? 🤔

Open Mike-E-angelo opened this issue 1 year ago • 11 comments

This issue piggybacks on this one: https://github.com/seesharper/LightInject.Microsoft.DependencyInjection/issues/210

I am beginning to wonder... is this a bigger problem? Does LightInject need to support Keyed Services? 🤔

Mike-E-angelo avatar Aug 30 '24 18:08 Mike-E-angelo

Hi @Mike-E-angelo

The problem here is a little bit more complicated than it might seem at first glance. LightInject has always supported keyed/named services so one should think that this should work out of the box. The problem is that LightInject.Microsoft.DependencyInjection does some , should we call it "dirty tricks" to make LightInject compatible with MS.DI. 😃 Some of those tricks involves an internal naming of services so that we for instance can bring back services in their registration order. Take a look here https://github.com/seesharper/LightInject.Microsoft.DependencyInjection/blob/9e5424007878ff0cf4b54ab9c0ce9402b4a36bc6/src/LightInject.Microsoft.DependencyInjection/LightInject.Microsoft.DependencyInjection.cs#L118

It ain't pretty,but it has worked up until now where MS.DI itself has started to support keyed services. Then those "hacks" would no longer work in all cases and the adapter breaks down.

So to fix this I've started to do some changes to LightInject itself to better adapt to MS.DI without using LightInject's named services as a way to be compatible with MS.DI.

I've started this work some time ago, but since nobody has asked for it yet , I pretty much left it on the shelf knowing that I would have to pick up on it eventually.

I guess that time is now. But just as you know, there is no easy fix here that can be solved in the adapter. Changes has to be made in LightInject to accommodate this.

seesharper avatar Aug 30 '24 18:08 seesharper

We probably end up with a new option in LightInject. Something like ContainerOptions.EnableMicrosoftCompatibility or something like that. Once that is in place and it passes all the tests from MS.DI with the option enabled it should be fairly easy to modify the adapter. In fact the adapter would be much simpler since we don't need to "adapt" so much anymore. 😃

seesharper avatar Aug 30 '24 18:08 seesharper

That sounds awesome @seesharper thank you for taking the time to comment and inform. I have to admit I am doing double-takes here because I am now shaking my head that "hey! DI has always had named services." ... 🤔 So these keyed services are different? I am so confused. 😅

I am of course more than happy to help test any new features that help address this problem. As I mentioned in the other thread an error is thrown currently but soon it will be null and that might lead to other issues.

Mike-E-angelo avatar Aug 30 '24 19:08 Mike-E-angelo

Hi @Mike-E-angelo . Just wanted to let you know that I have been working on this quite a lot the last couple of weeks. I have all the spec tests from MS working now. Still some cleanup needed, but we're getting there. Should be ready by the time .net 9 goes RTM 👍🙂

seesharper avatar Sep 28 '24 10:09 seesharper

That's awesome @seesharper! Thank you so very much for all your efforts out there. 🙏

Mike-E-angelo avatar Sep 28 '24 13:09 Mike-E-angelo

Thanks a lot for sponsoring me. 🤩. Really appreciate it.

seesharper avatar Sep 28 '24 22:09 seesharper

Right back atcha @seesharper ... thank you for being so responsive and engaging with your product. It has been fundamental and utilized by my framework for many years and is the least I can do. 🙏

Mike-E-angelo avatar Sep 29 '24 07:09 Mike-E-angelo

@Mike-E-angelo Hi Mike. Just wanted to give you an update on the progress here. I'm pretty close to publishing a set of beta packages that you can try out. All tests are passing and we are looking pretty good I think. Minor cleanup left to do. Let you know as soon as the packages are available on Nuget. In the meantime, have a great weekend 👍🍻

seesharper avatar Oct 18 '24 16:10 seesharper

Awesome @seesharper thank you very much for the update. For your awareness, the project I was working that was using Aspire (where I ran into this issue) has been put on hold for now, so this is not a rush/priority at the moment, but it would of course be great to have it there when I need it. 😊🙏 I appreciate all your work out there and wish a great weekend for you as well. 👍

Mike-E-angelo avatar Oct 18 '24 17:10 Mike-E-angelo

Hi @Mike-E-angelo Just wanted to inform you that support for keyed services is now fully implemented.

The new packages are

  • LightInject 7.0.0
  • LightInject.Microsoft.DependencyInjection 4.0.0
  • LightInject.Microsoft.Hosting 2.0.0

Technically there are no breaking changes in these versions, but I decided to bump the major anyway 🙂

Again thanks for your sponsor contribution and for the encouragement ❤️

seesharper avatar Nov 07 '24 14:11 seesharper

That's really awesome @seesharper! Thank you again so very much for this. This will be very useful going forward to onboard the new tech such as Aspire which is using this feature. 🙏 I greatly appreciate all your efforts out there. 👍✨

Mike-E-angelo avatar Nov 07 '24 15:11 Mike-E-angelo