tags:

views:

594

answers:

4

I have a simple WCF duplex TCP service that I am trying to stop programmatically. If I don't have any connected users, ServiceHost.Close() is very quick but if I even have one connected user, I find the Close() function to take quite a bit of time, sometimes >30seconds. Is this usual behavior?

On the other hand, Abort() is almost instantaneous and I am tempted to use that instead.

+1  A: 

It may be. The docs state that

The Close method allows any unfinished work to be completed before returning. For example, finish sending any buffered messages.

There is an overload to Close() which takes a TimeSpan (and throws if the timespan is exceeded)

Abort() looks like the best way to stop a WCF host without delay.

Scott Weinstein
A: 

A stab in the dark if you're still stuck.... are there any sessions at work here? Say the user has a 1 minute timeout (after which they are classified as offline), is it possible that this may keep it alive until this timeout expires?

Tanner
A: 

If you are ok with killing any in-progress service calls then Abort() is the way to go. Close() is the polite way of closing down a service.

AgileJon
A: 

I could see the benefit of Abort() over Close() for expediency, but I would imagine something bad may happen. In my case, I want to wait for Close() so I can reuse the port(s). This code will wait for the services to be actually closed before resuming.

Semaphore cont=new Semaphore(0, 1);
foreach (ServiceHost s in WCFServices) {
    try {
        s.Closed += delegate(Object o, System.EventArgs n) {
            cont.Release();
        };
        s.Close();
        cont.WaitOne();                 
    } catch (Exception) {
    }//try
}//for
Kyle Lahnakoski
I am sure you can rearrange this so all the "s.Close()" are issued first, and then wait on the Semaphore.
Kyle Lahnakoski