I’m using a Prism and the MVVM pattern
I’ve got a service running in a repository that is registered in my container like this:
_container.RegisterType<ITheService, TheService>();
_container.RegisterType<IRepository<Order>, Repository>(new ContainerControlledLifetimeManager());
The constructor of the repository will subscribe to the service, that will then received new element (like 5 to 10 every second).
The problem is that because of this subscription, when i close the window of my GUI, the module holding the service is not closed, and the service keeps on running, instead of being shut down by the dispose.
If i wasn’t in MVVM i would just unsubsribe the service when i close the the window, but the shell window can’t do that.
I see 2 solutions
* have an eventaggregator that will let my repository know when the shell close, and then unsubscribe the service (not sure it would work though)
* resolve the repository from the shell (as it’s registered as a singleton), and unsubscribe when the service, but that would be very ugly….
So i tried my first idea, which was using an EventAggregator.
The shell publish an event in the EventAggregator when the Closed event is fired.
The repository holding the service has subscribed to that event, and will get notified when the shell close. So i just need to close the service when i get the notification…