This project has moved and is read-only. For the latest updates, please go here.

How destroy objects in IoC - created with MEF

Jan 2, 2011 at 5:46 PM

Hi I use MEF and caliburn.micro in WPF app. I would like know how can I destroy instaces created with MEF.

For example simple shell:


public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShellViewModel

protected override void OnInitialize()

//first screen
public void ShowLogOn()
var vm = IoC.Get<ILogOnViewModel>();

//second screen
public void ShowMessenger(Account account)
var vm = IoC.Get<IMessengerViewModel>();
vm.Account = account;


 First screen:


public class LogOnViewModel : Screen,ILogOnViewModel
User user=new User();

 Second screen:


public class MessengerViewModel : Screen, IViewModelIdentity,
IMessengerViewModel, IHandle<Rp>, IHandle<string>

WPF app start with first screen associated with ILogOnViewModel interface. Then I deactivate this screen and activate second screen associated with IMessengerViewModel interface.

I check memory usage with ANTS Mememory profiler but instace of ILogOnViewModel is still alive also instace of class User is alive.

I am newbie in IoC, DI... all class which are exported with MEF must live during the whole WPF application lifetime cycle?

Other example, I create new wpf window with window manager.


public class ChatViewModel : Screen, IViewModelIdentity,
IChatViewModel, IHandle<Rp>, IHandle<DetailData>

ChatViewModel is WPF Window.

Create instance of IChatViewModel:

private IWindowManager _windowManager;
var chatScreen = IoC.Get<IChatViewModel>();

Then I close (click on X (close) button on window) WPF Window, ChatViewModel is deactivated but instace of this class is still alive.

It exist way how kill/destroy this instaces?

Jan 3, 2011 at 2:26 PM

I've answered a similar question on stackoverflow.