views:

1189

answers:

3

Hi everyone,

I'm curious to know how I would go about setting up my service to stop cleanly on the server the service will be installed on. For example when I have many clients connecting and doing operations every minute and I want to shut-down the service for maintenance, how can I do this in the "OnStop" event of the service to then let the main service host to deny any new client connections and let the current connections finish before it actually shuts down its services to the client, this will ensure data isn't corrupted on the server as the server shuts down.

Right now I'm not setup as a singleton because I need scalability in the service. So I would have to somehow get my service host to do this independently of knowing how many instances are created of the service class.

I hope I explain myself good enough, if not, let me know, I'll try to explain better.

Thanks, Scott

+1  A: 

You just have to call Dispose on the ServiceHost instance that you create. Once you do that, you will not accept any more clients and the service will continue to finish the operations for clients that are already connected.

casperOne
Would "serviceHost.Close()" be called before "serviceHost.Dispose()" or do all I need is "serviceHost.Dispose()" as it'll take care of the .Close?
ScottN
@Sentax: They are the same. You can call either/or.
casperOne
My serviceHost doesn't have a .Dispose method, I'm calling the .Close method in the OnStop event. I tried this when a client was connected and doing things and it still shut down the server and the client was yet to be disconnected.
ScottN
@Sentax: ServiceHost implements IDisposable, which has a Disposable method. You might be running into issues with the timeout expiring when shutting down the service. If you want to forcably abort, then you have to abort on the service host (there should be an Abort method).
casperOne
A: 

I've been wondering the same thing. I found this article which has a pretty in-depth description of how to properly Close/Dispose as ServiceHost or Client.

http://www.danrigsby.com/blog/index.php/2008/02/26/dont-wrap-wcf-service-hosts-or-clients-in-a-using-statement/

Andy White
A: 

In order to accomplish this. I had to create a service reference of itself and in the Windows Service OnStop initiate a new connection and change values in the WCF Service to "shut down" (this was just a shared boolean that the service was online or offline) the service so new clients wouldn't be able to connect (A function the client would call to see if the server was online or offline) and the existing connections would have time to finish up, then after all clients disconnect, continue to shut down the WCF Service using the .Close method.

ScottN