您如何强制Unity创建新实例? [英] How can you force Unity to Create a new instance?
问题描述
使用Unity Application块,当我们在WCF中调用 UnityContainer.Resolve< T>()
方法时,如何强制Unity配置创建对象的新实例。
Using Unity Application block how can you force the Unity configuration to create a new instance of an object when we call the UnityContainer.Resolve<T>()
method in WCF context?
推荐答案
Unity 中的28v = pandp.20%29.aspx rel = noreferrer> Lifetime Manager 。默认情况下,Unity使用 TransientLifetimeManager
:
Lifetime Manager in Unity
is all what you need. By default, Unity use TransientLifetimeManager
:
TransientLifetimeManager。对于此生命周期管理器,Unity为每次对Resolve或ResolveAll方法的调用创建并返回所请求类型的新实例。除非您指定其他寿命管理器,否则默认情况下,此寿命管理器将用于所有使用RegisterType方法注册的类型。
TransientLifetimeManager. For this lifetime manager Unity creates and returns a new instance of the requested type for each call to the Resolve or ResolveAll method. This lifetime manager is used by default for all types registered using the RegisterType, method unless you specify a different lifetime manager.
如果需要要使用另一个生命周期管理器,只需在 Register
方法中指定:
If you need to use another lifetime manager, just specify in Register
method:
var container = new UnityContainer();
container.RegisterType<IMyType, MyType>(new PerResolveLifetimeManager());
这篇关于您如何强制Unity创建新实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!