Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 803
Simon Bull Avatar asked Oct 19 '25 03:10

Simon Bull


2 Answers

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.

like image 197
Dutts Avatar answered Oct 21 '25 21:10

Dutts


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.

like image 39
andris Avatar answered Oct 21 '25 21:10

andris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!