Solved

Kill WCF server

Posted on 2010-09-18
19
736 Views
Last Modified: 2013-12-16
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.
0
Comment
Question by:BlearyEye
  • 9
  • 5
  • 2
  • +1
19 Comments
 
LVL 14

Expert Comment

by:existenz2
ID: 33714216
You need to kill the process. That is the easiest way to do it. That will always kill it, even when it hangs.
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33718803
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?
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 33721195
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();



0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33721669
That would be pretty complex in this case. And in any event, it might be hung.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 33722056
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
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33724897
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.
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 33749710
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?
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33753862
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.

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 21

Expert Comment

by:surajguptha
ID: 33755771
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...
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33758326
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?
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 33758554
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.  
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33760846
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 ...
0
 
LVL 21

Expert Comment

by:surajguptha
ID: 33771525
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.
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33772753
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.
0
 
LVL 21

Accepted Solution

by:
surajguptha earned 500 total points
ID: 33772852
In the start up of each of your application log make it save it's own Process Number or Process Id to the database for a file alone with the port number/ address it is listening to. This way, everytime an application starts it will log information about itself to the database.

Add another logic to startup which will check the database to see if there are existing processes for the port/address you are launching. If you get any matches, get the process Id from the database, get the process by Process Id (Id is different from Process name and description) and kill it.
0
 
LVL 1

Author Comment

by:BlearyEye
ID: 33773179
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.
0
 
LVL 1

Author Closing Comment

by:BlearyEye
ID: 33794521
A plausible solution, though not the one I was looking for. But the one I want might not be possible.
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now