WCF ServiceContracts 应该始终是 IDisposable 吗?
由于 另一个问题,我正在考虑 IDisposable 和服务合同。
我的 FooService 代理类继承自 ServiceProxyBase,它实现了 IDisposable。我的代理类也继承自服务契约 IFooService。这是否意味着 IFooService 也必须是 IDisposable,以便我可以在需要 IFooService 的地方注入代理的实例并能够正确处置它?
I'm thinking about IDisposable and service contracts because of this other question.
My proxy class for FooService inherit from ServiceProxyBase, which implements IDisposable. My proxy class also inherits from the service contract IFooService. Does this mean IFooService has to be IDisposable as well, so that I can inject an instance of my proxy wherever an IFooService is needed and be able to dispose of it properly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不可以。如果您从实现 IDisposable 的类继承,则不应重新实现 IDisposable。
如果您有需要在 Dispose 时清理的对象,则可以重写受保护的 Dispose(bool) 方法,该方法应作为完整 Disposable 模式的一部分在 ServiceProxyBase 中实现(即使 IDisposable 未指定此方法)。 bool 值表示您正在因应用程序调用 Dispose() 而进行处置。如果 bool 为 false,则已从垃圾收集器内的终结器调用您,这意味着您仅清理非托管对象。
您的 Dispose(bool) 实现应通过调用 base.Dispose(bool) 来完成。
No. If you inherit from a class that implements IDisposable, you should not re-implement IDisposable.
If you have objects that need to be cleaned up at Dispose time, then you override the protected Dispose(bool) method, which should be implemented in ServiceProxyBase as part of the full Disposable pattern (even though IDisposable doesn't specify this method). The bool value indicates that you're disposing as a result of your application calling Dispose(). If the bool is false, you've been called from a finalizer inside the garbage collector, which means you only clean up unmanaged objects.
Your implementation of Dispose(bool) should finish by calling base.Dispose(bool).