Simon Bull
Simon Bull

Reputation: 881

PRISM/Unity IDisposable

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?

Upvotes: 4

Views: 1185

Answers (2)

andris
andris

Reputation: 76

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.

Upvotes: 1

Dutts
Dutts

Reputation: 6191

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.

Upvotes: 3

Related Questions