0

The one area of .NET that's always befuddled me is exactly when to implement the IDisposable pattern.

I have created a SOAP web service proxy using WSDL which implements IDisposable on the Component base class:

public partial class WSDLGeneratedProxy : System.Web.Services.Protocols.SoapHttpClientProtocol

I have created sort-of facade interface with simplified methods so that I could hide the service proxy interactions behind, and made it implement IDisposable:

public interface IServiceClient : IDisposable

I have an implementation of the IServiceClient and that implementation contains an actual member of WSDLGeneratedProxy

public class FacadeServiceClient : IServiceClient
{
    private WSDLGeneratedProxy clientProxy;
}

So my question is - should I call the Dispose method on the service proxy explicitly? Is that the proper way to do it?

public void Dispose()
{
    this.Dispose(true);
    GC.SuppressFinalize(this);
}

protected virtual void Dispose(bool disposing)
{
    if (disposing)
    {
        if (clientProxy != null)
        {
            clientProxy.Dispose();
        }
    }
}

Any suggestions or input is greatly appreciated.

Nate222
  • 856
  • 2
  • 15
  • 25

1 Answers1

-1

I don't see any reason for which you would have to dispose that object, it's going to be garbage collected anyways when it's no longer used.

IDisposable interface base use is to dispose unmanaged objects (check Proper use of the IDisposable interface)

That's the reason why we use using with for example System.IO classes like FileStream

      using (var fs = new FileStream(filePath, mode)) 
       {
              //use fs 
       }

When the compiler encounters using keyword it will rewrite the code into something like

        FileStream fs = new FileStream(filePath, mode); 
        try 
        {
            //use fs
         }
        finally 
        {
            //compiler will automatically call dispose on the FileStream to free up unmanaged objects
             fs.Dispose(); 
         }

You may also want to use dispose when you work with huge objects in memory (like hundreds of MB) and do not wish to wait for the GC to collect them but rather do it earlier.

If you're not in any of the mentioned above situations then the GC will for sure do better work in disposing the objects than you will do.

Community
  • 1
  • 1
Dan Dinu
  • 32,492
  • 24
  • 78
  • 114
  • Yes, I can see where you're coming from. The reason I originally posed the question was due to conflicting information such as outlined here (Rule #2): http://blog.stephencleary.com/2009/08/how-to-implement-idisposable-and.html wherein it states that if you have a class with a single managed resource (that implements IDisposable) then the containing class should in turn implement IDisposable. – Nate222 Nov 06 '13 at 23:34
  • -1 if your class is holding an `IDisposable` object for the lifetime of your object, you should always implement `IDisposable` yourself and dispose of that object in its disposal method. Somewhere down the line, *something* owns an unmanaged resource and it needs to be accounted for. http://stackoverflow.com/a/538238/3312 – Jesse C. Slicer Nov 07 '13 at 02:48
  • @JesseC.Slicer i don't see why the -1 i think it's pretty much clear what i commented: "IDisposable interface base use is to dispose unmanaged objects" => so it's obvious that you have an IDisposable object unmanaged resources might pe involved and you need to handle that. Plus, i posted a link to the much more complex answer (the same link as you did :|). – Dan Dinu Nov 07 '13 at 09:00
  • @DanDinu "I don't see any reason for which you would have to dispose that object" seems contradictory to what you just said in this comment. – Jesse C. Slicer Nov 07 '13 at 14:42