我有一個分爲PRISM模塊的WPF應用程序。Wpf PRISM一次性模塊
我有一些非託管資源交易服務,因此它實現了IDisposable接口:
public class MyCacheService : IMyCacheService, IDisposable
{
...
}
我也有註冊一個IMyCacheService實例CameraServicesModule:
public class CameraServicesModule : IModule
{
public CameraServicesModule(IComponentsManager manager)
{
this.manager = manager;
}
public void Initialize()
{
...
var theCacheService = new MyCacheService();
this.manager.RegisterInstance(typeof(IMyCacheService), null, theCacheService);
}
}
的問題是:我如何處置服務實例?我必須在應用程序關閉時執行此操作,但現在MyCacheService.Dispose()在應用程序關閉時(或任何其他時間點)不會被調用。
CameraServicesModule是否應該實現IDisposable?像這樣:
public class CameraServicesModule : IModule, IDisposable
{
public CameraServicesModule(IComponentsManager manager)
{
this.manager = manager;
}
public void Initialize()
{
...
this.theCacheService = new MyCacheService();
this.manager.RegisterInstance(typeof(IMyCacheService), null, theCacheService);
}
public void Dispose()
{
this.theCacheService.Dispose();
}
}
如果是這樣,問題是:我該如何處置模塊?如果沒有,問題是:我應該選擇哪種方式來服務?
LocalFileSystemConfigurationManager configManager = new LocalFileSystemConfigurationManager(this.PathToSettings);
ComponentsManager compManager = new ComponentsManager(configManager, null, null);
注意:即使我的模塊不會實現IDisposable,處分componentsManager或ConfigurationManager中不配置模塊。