Remove event references in EventAggregator

Topics: Prism v4 - Silverlight 4
Dec 29, 2011 at 9:41 AM
Edited Dec 29, 2011 at 9:42 AM

Hi all,

I have an application using Prism and Silverlight 4. For events management, I use EventAggregator class.

In order to avoid memory leaks, I have seen that EventAggregator has a private property named "_events" that holds all event instances but I want to remove some of them when I perform logout.

How can I do it?


Thanks a lot.


Dec 29, 2011 at 4:24 PM


Based on my understanding Prism out of the box doesn't allow to directly remove items from this property, this seems to be by design. As you can find in the Communication Chapter in Prism documentation at msdn the EventAggregator is responsible for locating or building events and for keeping a collection of the events in the system. And by using its GetEvent method allows you to construct the event on its first access if it has not already been constructed.

Also I believe this shouldn't be the cause of a significantly memory leak, as the CompositePresentationEvent used by Prism maintains a weak delegate reference to the subscriber's handler. Unless you are using the keepSubscriberReferenceAlive parameter to specify a strong reference, the EventAggregator won't keep the subscribers alive.

On the other hand if you believe this might be helpful regarding your scenario, you could create your custom EventAgreggator class which derives from IEventAggregator. And replace the default one by overriding the ConfigureContainer method in the bootstrapper like in the following link:

Note: the property "_events" is used in Prism v2 EventAggregator, in Prism v4 is called "events". The classes are slightly different; however, they don't show a different behavior regarding this subject.

I hope you find this handy,

Agustin Adami