Web service - unclosed connections

I've been asked to take a look at a problem we're having with web service connections not being closed.

My understanding is that after 4 minutes the connections are closed by the server?

I guess the problem is that we are running out of available connections?

I am looking for some advice on some common things to look for in the C# source that may be causing the connections not to close.

How does garbage collection and web services work normally?

Is there a way to ensure that the socket connection closes immediately after the web service call is finished?

I was shown on the server what the unclosed connections look like:
netstat -bn

 TCP        ESTABLISHED     2360

 TCP        CLOSE_WAIT      1648

 TCP         CLOSE_WAIT      2632

///////////// these connections are not getting closed ///////////////////////////
 TCP         TIME_WAIT       0
 TCP        TIME_WAIT       0
 TCP          TIME_WAIT       0
 TCP          TIME_WAIT       0
 TCP          TIME_WAIT       0
 TCP          TIME_WAIT       0
 TCP          TIME_WAIT       0

Open in new window

From a scalability perspective, some of the people I have spoken to have suggested that this problem has only recently begun to happen because the web service is getting called more often.  So, bugs that you might get away with start to come to the surface that were not evident before.  They think that is what we are experiencing.
Tom KnowltonWeb developerAsked:
Who is Participating?
lojkConnect With a Mentor .Net and Infrastructure ConsultantCommented:
Not closing wcf and xml-ws calls is a very common problem, only exacerbated by the fact it is so easy to create the connection (I.e. A new client instance/connection each time) and these days we are all so used to garbage collection tidying up for us that we forget to do the right thing.

I try and reuse the connection where possible if likely to be frequently used (e.g data proxying ) but sometimes it is clearer to drop it and create a new one when you know the calls will be infrequent and inconsistent and/or you are sympathetic to or actually the guy running the web server hosting the service.
lojk.Net and Infrastructure ConsultantCommented:
I just posted a comment in another question that should answer this question for you quite nicely.. :-)

Tom KnowltonWeb developerAuthor Commented:
Thank you.

In case I can't find the sample code, can you show me in a code snippet how to close the connection?
lojk.Net and Infrastructure ConsultantCommented:
Just checked on my wcf4 service if you browse to the .svc url you get the sample..

here is the output of that page though, warts and all...


svcDataSync Service

You have created a service.

To test this service, you will need to create a client and use it to call the service. You can do this using the svcutil.exe tool from the command line with the following syntax:

svcutil.exe http://localhost/BytesStore/svcDataSync.svc?wsdl

This will generate a configuration file and a code file that contains the client class. Add the two files to your client application and use the generated client class to call the Service. For example:


class Test
    static void Main()
        IsvcDataSyncClient client = new IsvcDataSyncClient();

        // Use the 'client' variable to call operations on the service.

        // Always close the client.

Visual Basic

Class Test
    Shared Sub Main()
        Dim client As IsvcDataSyncClient = New IsvcDataSyncClient()
        ' Use the 'client' variable to call operations on the service.

        ' Always close the client.
    End Sub
End Class
Tom KnowltonWeb developerAuthor Commented:
Perfect...yes...I eventually found this.

Please see my new question:

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.