当我处理Unity IoC容器时,如何确定在单例依赖项(ContainerControlledLifetimeManager)上调用Dispose()的顺序?根据我的观察(以及基于考虑应该如何工作的我的期望):
任何人都可以确认我在正确的轨道上吗?互联网的其余部分在这个主题上似乎很安静,但是如果你能提供一个明确说明这一点的参考链接,我真的很感激。 TIA!
答案 0 :(得分:0)
对不起,您回答的太晚了(可能会对其他人有所帮助)。
从我的测试中,我可以确定容器是按反向解决顺序处理的! 因此,即使通过寄存器实例进行注册,处置也会与解析相关。
未解决的实例也会被处理,但是顺序却无法预测!?
测试(缺少对象和接口声明):
var unityContainer = new UnityContainer();
unityContainer.RegisterInstance<IDisposableObject>("ObjUnused1", new DisposableObject("ObjUnused1"));
unityContainer.RegisterInstance<IDisposableObject>("Obj1", new DisposableObject("Obj1"));
unityContainer.RegisterType<IDisposableObject, DisposableObject>("Obj2", new ContainerControlledLifetimeManager(), new InjectionConstructor("Obj2"));
unityContainer.RegisterInstance<IDisposableObject>("Obj3", new DisposableObject("Obj3"));
unityContainer.RegisterType<IDisposableObject, DisposableObject>("Obj4", new ContainerControlledLifetimeManager(), new InjectionConstructor("Obj4"));
unityContainer.RegisterType<IDisposableSubObject, DisposableObject>(new ContainerControlledLifetimeManager(), new InjectionConstructor("Sub"));
unityContainer.RegisterType<IDisposableObjectWihSub, DisposableObjectWithSub>(new ContainerControlledLifetimeManager());
unityContainer.RegisterInstance<IDisposableObject>("ObjUnused2", new DisposableObject("ObjUnused2"));
var obj1 = unityContainer.Resolve<IDisposableObject>("Obj1");
var obj2 = unityContainer.Resolve<IDisposableObject>("Obj2");
var obj3 = unityContainer.Resolve<IDisposableObject>("Obj3");
var obj4 = unityContainer.Resolve<IDisposableObject>("Obj4");
var objWithSub = unityContainer.Resolve<IDisposableObjectWihSub>();
objWithSub.Label = "Master";
Console.WriteLine($"\r\nContainerContent:\r\n{unityContainer.DumpRegistrationLines()}");
Console.WriteLine();
unityContainer.Dispose();
结果:
Creation DisposableObject - ObjUnused1 (RegisterInstance)
Creation DisposableObject - Obj1 (RegisterInstance)
Creation DisposableObject - Obj3 (RegisterInstance)
Creation DisposableObject - ObjUnused2 (RegisterInstance)
Creation DisposableObject - Obj2 (RegisterType - via Resolve)
Creation DisposableObject - Obj4 (RegisterType - via Resolve)
Creation DisposableObject - Sub (RegisterType - via Resolve)
Creation DisposableObjectWithSub - (RegisterType - via Resolve)
ContainerContent before Disposal:
IUnityContainer -> IUnityContainer ContainerLifetimeManager Microsoft.Practices.Unity.IUnityContainer
IDisposableObject -> IDisposableObject Singleton Name:"ObjUnused1" Test.Entities.IDisposableObject
IDisposableObject -> IDisposableObject Singleton Name:"Obj1" Test.Entities.IDisposableObject
IDisposableObject -> DisposableObject Singleton Name:"Obj2" Test.Entities.DisposableObject
IDisposableObject -> IDisposableObject Singleton Name:"Obj3" Test.Entities.IDisposableObject
IDisposableObject -> DisposableObject Singleton Name:"Obj4" Test.Entities.DisposableObject
IDisposableObject -> IDisposableObject Singleton Name:"ObjUnused2" Test.Entities.IDisposableObject
IDisposableSubObject -> DisposableObject Singleton Test.Entities.DisposableObject
IDisposableObjectWihSub -> DisposableObjectWithSub Singleton Test.Entities.DisposableObjectWithSub
Disposing:
Disposal DisposableObject - ObjUnused2
Disposal DisposableObjectWithSub - Master
Disposal DisposableObject - Sub
Disposal DisposableObject - Obj4
Disposal DisposableObject - Obj3
Disposal DisposableObject - Obj2
Disposal DisposableObject - Obj1
Disposal DisposableObject - ObjUnused1
结论:容器在处理时表现出色!