0

I have a following class:

public class MyClass : IDisposable
{
   private WebServiceHost m_WebServiceHost;
   // Members
   public void Dispose()
   {
            m_WebServiceHost // how do I dispose this object?
   }
}

WebServiceHost implements IDisposable, but it has no Dispose method.

How do I implement Dispose()?

Chris Mantle
  • 6,419
  • 3
  • 33
  • 48
Yakov
  • 9,221
  • 29
  • 109
  • 199

2 Answers2

3

Given that it uses explicit interface implementation, it's not clear to me that they want you to, but you can:

public class MyClass : IDisposable
{
   private WebServiceHost m_WebServiceHost;
   // Members
   public void Dispose()
   {
            ((IDisposable)m_WebServiceHost).Dispose();
   }
}

I would guess that they'd prefer you just to call Close() on it, but I can't back that up from documentation yet.

Damien_The_Unbeliever
  • 227,877
  • 22
  • 326
  • 423
2

Do it like this:

public class MyClass : IDisposable
{
   private WebServiceHost m_WebServiceHost;

   // Often you have to override Dispose method 
   protected virtual void Dispose(Boolean disposing) {
     if (disposing) {
       // It looks that WebServiceHost implements IDisposable explicitly
       IDisposable disp = m_WebServiceHost as IDisposable;

       if (!Object.RefrenceEquals(null, disp))
         disp.Dispose();

       // May be useful when debugging
       disp = null;       
     }
   }

   // Members
   public void Dispose()
   {
     Dispose(true);
     GC.SuppressFinalize(this);
   }
}
Dmitry Bychenko
  • 165,109
  • 17
  • 150
  • 199