DataContext IRegionMemberLifetime.KeepAlive

Topics: Prism v4 - WPF 4
Jun 15, 2011 at 3:19 PM


I use the regionMemberLifetime attribute to control the view behaviour. When set to False, the view is destroyed. So far so good.

However, each time I recreate the view, I get a new viewmodel that gets instantiated. The viewmodel IRegionMemberLifetime.KeepAlive is never called and I get a new thread each time. This doesn't seem to be consistent with the documentation available here :

Any idea on how to trigger viewmodel destruction when view is destroyed ?




Jun 15, 2011 at 6:09 PM


It could be helpful if you could provide us with a repro sample (for example, uploading to SkyDrive) of your solution, so that we can help you find out the cause of the unexpected behavior you're experiencing.


Miguel Bronzovic


Jun 16, 2011 at 2:24 PM

Hi Miguel,

You can find a code sample and a description on 

Please let me know if you need clarification.



Jun 16, 2011 at 9:13 PM

Hi Fabien,

We’ve been able to reproduce your scenario as you described it. One possible reason why your view model is not been garbage collected could be that another composable part of your application stills have a reference to the view model class.

We’ve debug your sample with Prism’s code, when you deactivate your ListeningControl View, it been removed from the ActiveView’s collection and also is causing to be removed from the Region. This is the expected behavior. This means, your views’s reference is no longer required and can be garbage collected including it datacontext references (ListeningControlViewModel).

You could check the EventBus class, since it’s been registered in the container as singleton (PartCreationPolicy.Shared).


Miguel Bronzovic


Jun 17, 2011 at 11:42 AM
Edited Jun 17, 2011 at 2:37 PM

Hi Miguel,

Thanks for your quick answer, that's indeed the issue. When replacing with IEventAggregator, I don't get the issue. 

I guess I'd need to better understand how IEventAggregator is setup.



EDIT : It takes time for the garbage collector to be called. Usually I still receive a few messages in the LeadingControl View, and then it stops. However when using webservices in the viewmodel, I now get other issues. 

Errors like : 

- "Cannot access a disposed object. Object name: 'System.ServiceModel.Channels.HttpChannelFactory+HttpRequestChannel'."

OR "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server."

(while each time I call a service, I close it right after). 

So it seems the garbage collection interacts in some way with my service calls, making them fall, and I'm still trying to figure out how to make several modules talk to each other in a decoupled way, with viewmodels subscribing to messages and calling webservices to get data whenever required. 

Jun 17, 2011 at 5:48 PM

Doing an unsubscribe action seems to work (at least from preliminary tests).