我有一些模块在关闭时需要做一些整理工作,但是 PRISM/Unity 似乎不尊重 IDisposable 接口。有人对我如何使它工作有任何建议吗?
问问题
1020 次
2 回答
3
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.
于 2013-05-09T10:28:52.130 回答
1
很可能您的模块没有被释放,因为它们在容器中注册为单例(共享)组件。
Dispose()
手动打开您的容器Application.Exit
,并且您的所有一次性模块(以及该容器中其他已解析的共享一次性组件)都应该IDisposable.Dispose()
调用它们的方法。
于 2016-11-16T12:18:35.117 回答