开发者

Should WCF ServiceContracts always be IDisposable?

开发者 https://www.devze.com 2023-03-03 12:15 出处:网络
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 inh

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开发者_如何学C. 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?


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号