我有一个分为 PRISM 模块的 WPF 应用程序。
我有一个处理一些非托管资源的服务,因此它实现了 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 也不会处置该模块。