seesharper / LightInject.Microsoft.DependencyInjection

Implements Microsoft.Extensions.DependencyInjection.Abstraction
MIT License
46 stars 13 forks source link

Type registered on the LightInject container not resolved on the service provider #208

Open bounav opened 5 days ago

bounav commented 5 days ago

The following code example (I'm targetting .net 8.0 FYI) compiles but at runtime the variable bar is null.

        public interface IFoo {}

        public class Foo : IFoo { }

        public interface IBar { }

        public class Bar : IBar { }

        [Test]
        public void TestUseMicrosoftDiAndLightInjectSideBySide()
        {
            // var builder = Host.CreateApplicationBuilder();

            var services = new ServiceCollection();

            services.AddSingleton<IFoo, Foo>();

            var containerOptions = new ContainerOptions().WithMicrosoftSettings();

            var container = new ServiceContainer(containerOptions);

            container.Register<IBar, Bar>();

            // "Bridge" LightInject and Microsoft DI
            var serviceProvider = services.CreateLightInjectServiceProvider();

            var foo = serviceProvider.GetService<IFoo>();
            var bar = serviceProvider.GetService<IBar>();

            Assert.That(foo, Is.Not.Null);
            Assert.That(bar, Is.Not.Null);
        }

Is this by design?

My understanding is the point having this integration is so that you can register types with either Microsoft DI or LightInject and it just works because CreateLightInjectServiceProvider() ties the LightInjectContainer inside the Microsoft one?