Skip to content

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

@bounav

Description

@bounav

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions