views:

170

answers:

4

When you create and use a Web Service proxy class in the ASP.Net framework, the class ultimately inherits from Component, which implements IDisposable.

I have never seen one example online where people dispose of a web proxy class, but was wondering if it really needs to be done. When I call only one method, I normally wrap it in a using statement, but if I have a need to call it several times throughout the page, I might end up using the same instance, and just wondered what the ramifications are of not disposing it.

+3  A: 

You need to dispose everything that implements IDisposable.

The implementation of IDisposable signifies that the object holds onto something that the garbage collector doesn't intrinsically track - might be a connection, might be a file, might be some other handle - or might be nothing at all, it doesn't really matter. You shouldn't concern yourself with the implementation details because those are subject to change.

The class may or may not truly use unmanaged resources. It may or may not have a finalizer. It makes little difference; if the class implements IDisposable, then it's asking you to Dispose it when you're done. Even if the Dispose method does nothing, you never know when somebody will replace the reference to that class with a reference to a subclass that really does need to be disposed.

Aaronaught
+1  A: 

Sometimes Dispose() is just a dummy (inherited from a base class), but then it is still a good practice to call it, to be safe for future changes.

A WebService/WCF proxy is holding a connection, so it is certainly a good idea to call Dispose() or Close(). Doing so inside a using block is of course preferable since it is exception safe.

But in your case (using the proxy in multiple methods on your page) the use of multiple using blocks probably would take a performance hit. It is OK to replace the using blocks with a call Close() in an event that comes late in the page cycle. I'm not sure about ASP.NET best practices here, but I would use OnPreRender or OnPageUnload or something.

You will loose exception safety here but that is not a fundamental problem, the GC will solve that.

Henk Holterman
A: 

IMHO, there's nothing like disposing a web-service proxy class.

Most of the proxies are:

  • Stateless, as such calling using one instance or using multiple instances does not make any difference
  • Intermittent meaning that they close the connection as soon as all response is processed
MasterGaurav
A: 

You have to dispose the proxy class because resources are still in use when the response is processed.

For example, a connection between the proxy and the server remains active for the Keep-Alive and because you cannot have more than 2 or 4 active connections to the same server address for a workstation, further instances creations will fail if the previous proxies are not closed or garbage collected.

Lionel Schiepers.