views:

2161

answers:

4

Does the garbage collector clean up web service references or do I need to call dispose on the service reference after I'm finished calling whatever method I call?

+9  A: 

A much better alternative to worrying about disposing your web services would be to keep only a single instance of each web service, using a singleton pattern. Web services are stateless, so they can safely be shared between connections and threads on a web server.

Here is an example of a Web Service class you can use to hold references to your web service instances. This singleton is lazy and thread-safe. It is advised that if you make your singletons lazy, they are also kept thread safe by following the same logic. To learn more about how to do this, read Microsoft's documentation on Implementing Singletons.

Also keep in mind that you may run into issues with WCF web services. I'd recommend reading up on WCF's instance management techniques article, specifically the singleton section, for more details.

public static class WS
{
    private static object sync = new object();
    private static MyWebService _MyWebServiceInstance;

    public static MyWebService MyWebServiceInstance
    {
        get
        {
            if (_MyWebServiceInstance== null)
                lock (sync)
                    if (_MyWebServiceInstance== null)
                        _MyWebServiceInstance= new object();
                return _MyWebServiceInstance;
        }
    }
}

And then when you need to access your web service, you can do this:

WS.MyWebServiceInstance.MyMethod(...)

or

var ws = WS.MyWebServiceInstance;
ws.MyMethod(...)

note: For anyone who stumbled upon this answer, view the history of this post to see my previous answer, which included a simple benchmark test for web service performance hits when calling Dispose().

Dan Herbert
Thanks! I'll definitely implement a using now.
BeaverProj
@DanHerbert, we have revision control on your question publically available.. so you don't need to clutter your post with a ton of crossed out stuff. It is distracting
Simucal
Can you write us a whole book on this ? your almost there...
Jobo
So it actually reduces performance? Yikes. OK. Rethinking this. Performance is more important than making sure the memory is freed right away.
BeaverProj
Dan, is a webservice always thread safe when using singleton pattern? I am writing a wrapper for a 3rd party web service, should I be thinking about making my wrapper a singleton? Thanks
fearofawhackplanet
@fearofawhackplanet You probably should be. Web services are thread safe, but you have to make sure the singleton itself is thread-safe on your own. See the link I included to Microsoft's documentation in implementing thread-safe Singletons http://msdn.microsoft.com/en-us/library/ms998558.aspx
Dan Herbert
+1  A: 

I think the DataService inherits Dispose from Component.

BeaverProj
A: 

what are you trying to accomplish here?

If your worried about performance, then I would worry more about the responsiveness of the server hosting the webservice and the network speed, as they can dramatically affect the length of time you have to wait for the webservice call to complete (unless its asynchronous).

The examples on MSDN dont call 'Dispose' and its quite obvious that the garbage collector will do its job, so unless your working on a realtime system that needs to process over 100,000 records in memory every second, then maybe you dont need to come up with a way to dispose resources or manage memory.

Jobo
My question was more from a *should* you do it standpoint as apposed to a do you have to do it standpoint. My main concern is memory leaks which would slowly degrade performance over time.
BeaverProj
A: 

Objects that implement IDispose should be disposed of manually to assist the garbage collector.

If you object is short lived use a using block. For objects that can be retained ensure that they object that retains them disposes of them when it is also disposed.

orj