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

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

Closed
laxmanrapolu opened this issue Jan 23, 2020 · 1 comment
Assignees
Labels
Requires Investigation 👮 Requires further verification

Comments

@laxmanrapolu
Copy link

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.

@laxmanrapolu laxmanrapolu changed the title Second child container not resolving type properly Second child container not resolving type properly with Hierarchical Lifetime Jan 23, 2020
@ENikS ENikS transferred this issue from unitycontainer/unity Apr 22, 2020
@ENikS ENikS self-assigned this Apr 22, 2020
@ENikS ENikS added the Requires Investigation 👮 Requires further verification label Apr 22, 2020
@ENikS ENikS added this to the 5.11.7 milestone Apr 22, 2020
@ENikS ENikS removed the Requires Investigation 👮 Requires further verification label Apr 22, 2020
@ENikS
Copy link
Contributor

ENikS commented Apr 22, 2020

Version 5.2.1 was somewhat 'rushed' and had multiple issues. I would recommend to upgrade to the latest.

@ENikS ENikS closed this as completed Apr 22, 2020
@ENikS ENikS added master Issue has been merged into ‘master’ release Release branch and removed master Issue has been merged into ‘master’ release Release branch labels Apr 23, 2020
@ENikS ENikS removed this from the 5.11.7 milestone Apr 23, 2020
@ENikS ENikS added the Requires Investigation 👮 Requires further verification label Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Requires Investigation 👮 Requires further verification
Projects
None yet
Development

No branches or pull requests

2 participants