You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I need your help to understand if the following behavior is the expected one or if there is a trick to make it work. It seems that when there are nested dependency injections, the scope registrations are not taken into account. To make it clearer, I was expecting this test to pass, but it does not. It is failing to the last assertion:
[TestMethod]
public void NestedInjection_CreateChildContainer()
{
IUnityContainer unityContainer = new UnityContainer();
var serviceCollection = new ServiceCollection();
serviceCollection.AddScoped<DbContext>(p => new DbContext());
serviceCollection.AddScoped<Repository>(p => new Repository(p.GetService<DbContext>()));
serviceCollection.BuildServiceProvider(unityContainer);
// Creating a child container via unity
var repositoryInstance1 = unityContainer.CreateChildContainer().Resolve<Repository>();
var repositoryInstance2 = unityContainer.CreateChildContainer().Resolve<Repository>();
// Instances of repository should be different
Assert.AreNotEqual(repositoryInstance1.GetHashCode(), repositoryInstance2.GetHashCode());
// Instances of dbcontext should be different
Assert.AreNotEqual(repositoryInstance1.Context.GetHashCode(), repositoryInstance2.Context.GetHashCode());
}
My question is: does unity container creates a new scope when creating a child container?
@Naun
AddScoped registers the service with container lifetime. If you want the child container to have its own instance, then register the service in the child container, not the root, or make sure the service is registered in unity container with hierarchical lifetime
Thank you for your answer. But, that does not explain:
Why Repositories instances are differents whereas they are also registered as Scoped?
And additionally, if I write serviceCollection.AddScoped<DbContext>(); instead of using the lambda expression, the unit test passes: it provides two different instances of DbContext... Why?
Hi,
I need your help to understand if the following behavior is the expected one or if there is a trick to make it work. It seems that when there are nested dependency injections, the scope registrations are not taken into account. To make it clearer, I was expecting this test to pass, but it does not. It is failing to the last assertion:
My question is: does unity container creates a new scope when creating a child container?
Full test class:
NestedInjectionTests.log
The text was updated successfully, but these errors were encountered: