views:

48

answers:

1

Can anyone recommend a "clean" way of closing a WCF service and its threads when hosted in a servicehost object?

Calling servicehost.Close(); doesn't work when the service has spawned other threads :/

Cheers,

Rob

+1  A: 

That's really something you cannot do from the outside safely without collaboration from the service itself and the threads it spawned. One option would be to have the service spawn threads through special means that can be controlled through your host environment and have those threads furthermore collaborate so that they can be shutdown cleanly.

For example, you could have a custom thread pool class that gives each thread spawn a reference to an event that signals to it that it must stop processing and shutdown. .NET 4.0 is going to make this simpler with the Task Library, but meanwhile you're going to have to do it on your own.

Also, you should take care if you're spawning raw threads (instead of using the CLR thread pool) to create them as background threads in cases like this. This can help in avoiding keeping the process alive when you want it to shut down (though I'd still recommend making sure you shut them down cleanly).

tomasr
Thanks tomasr, a bit of thought brought me to the same conclusion.
theGecko