Link to home
Start Free TrialLog in
Avatar of BlearyEye
BlearyEyeFlag for United States of America

asked on

Kill WCF server

This ought to be easy, but I haven't found a way to do it. I need to launch a WCF server in such a way that if an instance is already running, it will kill the running one first.

Of course, by attempting to launch the server a second time, it will throw an exception that I can catch. But I don't know how to actually shut down the running instance. I could send it a shut-down message, but that will work only if the server responds correctly. I want a way to do it unilaterally.

I'm using C# 4.0 / VS 2010.
Avatar of existenz2
existenz2
Flag of Netherlands image

You need to kill the process. That is the easiest way to do it. That will always kill it, even when it hangs.
Avatar of BlearyEye

ASKER

So now my problem is to identify the process. I'll have several instances of this server running, each configured to listen on a different address.

I can get a list of running processes and can filter them for those that have the right name. But otherwise I don't see a good way to identify the one I need to kill since each instance will have the same name. Apparently I can check the title of the main window of the process, so perhaps I could kludge it that way.

Any other approach?
Avatar of Miguel Oz
Is there any reason to restart the server as a process?
I think it would be better if you have a RestartServer method, that clean up any cache or session objects.
so your client code will be:
 proxy.Restart();
 proxy.DoSomething();



That would be pretty complex in this case. And in any event, it might be hung.
Not really, why should your code hang?
If you put more details about your code and reaosns for hanging, I will be able to help you there.
Restarting a process is more of a brute force approach to hide the issue that the code can not clean itself properly.
You can even use a bool parameter to make just one call instead of two calls.
void Domething(bool cleanup)
{
  if (cleanup)
    //cleanup your code, set to null, close files, connections and force garbage collection. Use a sync object like mutex/lock to avoid other web service calls to access the variables while you are cleaning up.
  //Your code here
}

Searvers are solid code that runs 24/7
Servers can hang for a lot of reasons: deadlock, infinite loops, uncaught exceptions ...  Servers are not necessarily "solid code that run 24/7"; restarting a server is not that uncommon and in some cases is done routinely in order to clean up the server state. Restoring to original state from within the server can be very difficult.

Rather than trying to talk me out of it, how about trying to answer my original question? I need to identify running servers so that I can selectively kill them.
Hey, Can you explain a little more about the problem, like...
What you mean by " I need to launch a WCF server"?
Is there some code you are using to launch wcf server?
What is your WCF service's configuration?
By WCF Server, do you mean W3WP process?
I have a client program that launches up to N servers, each of which is listening via WCF on a different address (it's HTTP://localhost:port); not W3WP. This is all local to a single machine. At the time the client program wants to launch server k, it is possible that there is a WCF server already listening on k's address, possibly in a bad state.

I'd like to find out what process this is so that I can kill it. The server program is written in such a way that a restart is a reasonable way to restore server state. I know I can get a list of running processes and can filter them by name, but I don't know how to associate a process with a particular instance of the server.

If the client program has a handle to all the N servers launched, you could check for the health of the server by checking its state and determining if needs to be relaunched.

If you do not have a handle to all the end point listeners/ servers you created
1) The best way to do it is to get a list of running processes. In order to differentiate one process from the other you could change the process's description as the port/address it is listening to.
2) Maintain a central list file into which each of your servers will write to. Each server will write its own processId and the address it is listening to. When each server starts it will first check the central file to see if the address it is going to listen to is already worked upon by some other process. If there is already an entry, it will get the process id of the old process, terminate it before starting its own self.

Hope this helps...
I had thought about that, but didn't see a way to add a description to a process when it begins. Can you give me the syntax?
I thought that manipulating the EXE's description wouldn't be a big deal but I couldnt figure it out. The application sets the description as the name of the application automatically.

Since we couldn't figure out how to set description, I guess option(2) would be another way to proceed with this.  
Since processes can have separate descriptions, I thot it would be easy to do. But, C# is surprisingly deficient in some areas (managing z-order is another).

I'm doing #2 already. I was trying to handle the case where there was a "zombie" server hanging around. Looks like I'm stuck ...
You are stuck with the implemention of #2? If so, do you mean you are stuck with #2 because of Zombie server problem?

For Zombie server problems in general. There are two methods
Push : Every server updates it's own health with heartbeats every few minutes(HeartBeat Interval) and if the heartbeat is not received, the server is assumed to be dead and recycled.

Pull : A process gets the status of the server by sending it a request and if a response is not received, it is terminated.

You can choose either of these methods too.
My problem is that all the zombile servers have the same name. Within C# I can't find a way to distinguish them. Hence I can build a list of the servers but I don't know which one of them has to be killed.

That is, I can have several processes all named "My Server" each listening on a different channel. In C# I can make a list of them. One of them, listening on channel 3, say, is zombie. I know that the server listening on channel 3 is a zombie. But I don't know how to associate that fact with a particular process.
ASKER CERTIFIED SOLUTION
Avatar of surajguptha
surajguptha
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Your suggestion to use a database is not unreasonable. I had thought to use some kind of persistent storage. this depends on the integrity of the database, of course.

i had hoped for something more direct, such as tagging the server descriptions with their channel number, but this for some reason does not seem to be possible.
A plausible solution, though not the one I was looking for. But the one I want might not be possible.