3
votes

I have some modules that need to do some tidy up work when they are closing, however it would appear that PRISM/Unity does not respect the IDisposable interface. Does anyone have any suggestions on how I can get this working?

2
Did my answer help you at all mate?Dutts

2 Answers

3
votes

I experienced the same issue, and solved it like this:

First I created a custom Event to allow me to signal my modules that the container is closing:

public class ApplicationExitEvent : CompositePresentationEvent<string> { }

Then in my bootstrapper I implement IDisposable and fire the event in my Dispose() method:

    public void Dispose()
    {
        var eventAggregator = Container.Resolve<IEventAggregator>();
        if (eventAggregator != null)
        {
            eventAggregator.GetEvent<ApplicationExitEvent>().Publish("");
        }
    }

Then in my module's Initialize() method I subscribe to this event:

EventAggregator.GetEvent<ApplicationExitEvent>().Subscribe((o) => Dispose(), true);

And put whatever cleanup code I need in my module's Dispose method.

Hope this helps.

1
votes

Most probably your modules are not disposed because they are registered as singleton (shared) components in the container.

Dispose() your container manually on Application.Exit, and all your disposable modules (and other resolved shared disposable components from this container) should have their IDisposable.Dispose() method called.