Why does Unity create multiple instances?

Topics: Prism v2 - WPF 4
Jul 21, 2010 at 5:14 PM

I use PRISM v2.2 UnityContainer to hold the instance of MyView. When I call the second time of Resolve method, should I expect to get the return of the same instance from the Unity? The problem is the constructor of MyView is called twice. Every time I call Resolve method, I get a new instance.

var view1 = _container.Resolve<MyView>(“MyView”);
var view2 = _container.Resolve<MyView>(“MyView”);

What’s the correctly way to let the Unity return the same instance?

 

Jul 21, 2010 at 5:54 PM

By default, Unity will return a new instance when you ask for a concrete type or an interface that has been registered with the container.

This doc: http://msdn.microsoft.com/en-us/library/ff647489.aspx should help with the syntax on registering singletons.  Scroll down to To register a class or type as a singleton instance or To register an existing object as a singleton instance

If that does not help, let me know.

Michael Puleio