Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different behavior between LightInjectServiceProvider and Microsoft.Extensions.DependencyInjection.ServiceProvider regarding resolving IServiceProvider #169

Open
anlampe opened this issue Dec 10, 2020 · 0 comments

Comments

@anlampe
Copy link
Contributor

anlampe commented Dec 10, 2020

The LightInjectServiceProvider shows a different bahvior than the Microsoft.Extensions.DependencyInjection.ServiceProvider regarding resolving IServiceProvider.

I've created a test case which hopefully clarifies the desired behavior:

using System;
using LightInject;
using LightInject.Microsoft.DependencyInjection;
using Microsoft.Extensions.DependencyInjection;
using Xunit;

public class ServiceProviderTest
{
    public class Service { }

    [Fact]
    public void ShouldBeRegisteredPerScopeAndContainItself()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddScoped<Service>();

        var container = new ServiceContainer(ContainerOptions.Default.WithMicrosoftSettings());

        var serviceProvider = container.CreateServiceProvider(serviceCollection);
        //var serviceProvider = serviceCollection.BuildServiceProvider();

        using var scope = serviceProvider.CreateScope();

        var scopeServiceProvider = scope.ServiceProvider.GetRequiredService<IServiceProvider>();

        Assert.Equal(scopeServiceProvider, scope.ServiceProvider);
        Assert.Equal(scopeServiceProvider.GetRequiredService<Service>(), scope.ServiceProvider.GetRequiredService<Service>());
    }
}

If you uncomment the line var serviceProvider = serviceCollection.BuildServiceProvider(); which uses the Microsoft.Extensions.DependencyInjection.ServiceProvider you will see the test passes.

Is there a way to change the behavior of the LightInjectServiceProvider accordingly?

Besides that, thank you for your great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant