1

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?

Community
  • 1
  • 1
Patrick Szalapski
  • 8,738
  • 11
  • 67
  • 129

1 Answers1

2

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

Cylon Cat
  • 7,111
  • 2
  • 25
  • 33
  • OK, that makes sense. Then I should new up my service proxy (or use a proper IoC container to do so), pass it in whenever I need an instance of IFooService, and then dispose of it properly in the same class that new'ed it up. – Patrick Szalapski May 05 '11 at 21:40