Skip to content

Second child container not resolving type properly with Hierarchical Lifetime #209

@laxmanrapolu

Description

@laxmanrapolu

We have been using Unity version older than 5.2.1 and now started upgrading to newer versions (5.7.0).

Our previous code was working fine because of this issue #35 , but with the upgrade it started breaking.

We followed the suggestion mentioned in the above issue of registering concrete class explicitly. It worked fine when we have one child container, but is not working with second child container.

Below is the code

public class Program
{

public interface IUnitOfWork
{
}

public class Dtrcontext : IUnitOfWork
{
		
}


public void Main()
{
	IUnityContainer container = new UnityContainer();
	container.RegisterType<IUnitOfWork, Dtrcontext>(new HierarchicalLifetimeManager());
	container.RegisterType<Dtrcontext>(new HierarchicalLifetimeManager());
	var child1 = container.CreateChildContainer();
	var child2 = container.CreateChildContainer();
	
	var a = container.Resolve<Dtrcontext>();
	var b = child1.Resolve<Dtrcontext>();
	var c = child1.Resolve<IUnitOfWork>();
	var d = child2.Resolve<Dtrcontext>();
	var e = child2.Resolve<IUnitOfWork>();
	
	Console.WriteLine("a=b?" +(a==b ).ToString());
	Console.WriteLine(" b=c? " +(b==c ).ToString());
	Console.WriteLine(" d=e? " +(d==e ).ToString());
	Console.WriteLine(" b=d? " +(b==d ).ToString());
}

}

Result :
a=b?False
b=c? True
d=e? False --> This is expected to be true.
b=d? False

Fiddler link -> https://dotnetfiddle.net/Uka9UQ

Wanted to know if this is a know issue and is there any solution.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions