microsoft-dependency-injection icon indicating copy to clipboard operation
microsoft-dependency-injection copied to clipboard

A scoped service added only to a nested container is resolvable through a non-related nested container

Open Amleto opened this issue 3 years ago • 0 comments

Scenario: Root container with two child containers (siblings). A scoped service is added to one of the child containers. The service should only be resolvable from the child container in which it was registered.

This is the case in the following test, using IServiceProviderFactory<IUnityContainer>.CreateBuilder(...)

[Fact]
public void ScopedServiceOnlyResolvedFromChildContainer_using_CreateBuilder()
{
    var container = new UnityContainer();
    var factory = new ServiceProviderFactory(container);
    var unityFactory = (IServiceProviderFactory<IUnityContainer>) factory;

    var childServices = new ServiceCollection();
    childServices.AddScoped<SampleClass>();
    var childContainerA = unityFactory.CreateBuilder(childServices);
    var childContainerB = unityFactory.CreateBuilder(new ServiceCollection());

    Assert.True(childContainerA.IsRegistered<SampleClass>());
    Assert.False(childContainerB.IsRegistered<SampleClass>());
}

However, the following test using ServiceProviderFactory.CreateServiceProvider(...) fails. This is not expected.

[Fact]
public void ScopedServiceOnlyResolvedFromChildContainer_using_CreateServiceProvider_serviceCollection()
{
    var container = new UnityContainer();
    var factory = new ServiceProviderFactory(container);

    var childServices = new ServiceCollection();
    childServices.AddScoped<SampleClass>();
    var childContainerA = factory.CreateServiceProvider(childServices);
    var childContainerB = factory.CreateServiceProvider(new ServiceCollection());

    Assert.NotNull(childContainerA.GetService<SampleClass>());
    Assert.Null(childContainerB.GetService<SampleClass>()); // << container B also resolves the service, failing the test.
}

For reference,

private class SampleClass
        {
        }

Amleto avatar Apr 12 '21 16:04 Amleto