I implemented a small WCF service in .net 3.5, on which clients connect over TCP to port 4321. This service can spawn other processes (via System.Diagnostics.Process). Of course, when the service is killed or out of order or something else, unresolved processes remain. The problem is that if I try to restart the service while these processes are still running, I get the following exception:
CommunicationException: A listener already exists on the IP endpoint 0.0.0.0-00-00321. Make sure that you are not trying to use this endpoint several times in your application and that there are no other applications listening to this endpoint.
In fact, when I run netstat, I see that there is a process that listens on TCP port 4321:
TCP 0.0.0.0-00-00321 MTL-WKS-AG196: 0 LISTENING 97308
The process identifier that you see here (97308) is the service that I first started (which should no longer exist since it was killed). The only way to free a port is to kill all processes that were created during the life of the service.
I know little about ports and processes, but I understand that a child process "inherits" the ports that the parent process listens to. Is this more or less what is happening?
Is there any way to undo this behavior? Without access to the code of the generated process?
Also, I really don't think this is possible, but is there a way to tell Windows to kill child processes when their parent process is killed?
Thanks!
source share