views:

587

answers:

1

I have a WCF Service that I have written, which is hosted within a Windows service. It is operating in PerSession mode. The service permits clients to open files, make changes to files and close files remotely through the service. So far all works very smoothly.

When the Windows service is stopped, I would like to be able have the WCF Service not accept any new sessions and yet allow already connected clients to complete their current sessions and work (within a reasonable period/timeout).

What would be the best way to accomplish this?

+4  A: 

Basically, calling ServiceHost.Close() will accomplish this - it will let all currently running request run to completion, but new request are being rejected will the orderly shutdown is in progress.

There's a "CloseTimeout" setting on your binding that you configured for your service - that controls how long the WCF runtime will allow running requests to take until they've properly shut down. If that timeout happens, the still running requests will be killed.

The ServiceHost.Abort() method on the other hand is the digital equivalent of a sledgehammer - all running requests are terminated at once, and the host is shut down.

ON the client side, you have the same .Close() and .Abort() methods on your client proxy which behave the same way.

marc_s
calling ServiceHost.Close causes my service to immediately close client connections. I do not have the orderly shutdown that I desire. Is there some configuration that modifies this behavior?
David Chappelle
@David: no, I'm not aware of any settings to turn this on - this should be the default behavior - any service call that's still under way should be finished up orderly. However, this does not keep existing sessions open until they're done - those will be shut down if no service call is currently running.
marc_s
@marc_s: What I am seeing is that ServiceHost.Close allows the ServiceHost to complete any service calls in progress - BUT my clients have already received a System.ServiceModel.CommunicationException and never get the response. Is this what you'd expect? Any workarounds? It is fine if the clients get an exception when making new service calls, but any service calls that were running when ServiceHost.Close was called need to complete and return the expected values to the client.
David Chappelle
@David: that is my understanding: currently executing service calls will be finished and completed normally, while new ones will be rejected. I don't know of any settings to influence that or any workarounds, sorry.
marc_s
This is what the docs (http://msdn.microsoft.com/en-us/library/ms405496.aspx) say: " The Close method allows any unfinished work to be completed before returning. For example, finish sending any buffered messages. "
marc_s
Yes, the docs do seem to indicate that calling ServiceHost.Close() is the key, but in fact it closes the sessions right away, which is the worst possible outcome in my application. (Financial transactions, money is debited, but customer is not given the merchandise). Currently looking at deriving from ServiceHost and overriding OnClose() to do a subset of what ServiceHost.Close() does.
David Chappelle

related questions