Exception on System.ServiceModel.AddressAlreadyInUseException (ChannelListener)
I have an application which tries to host WCF service in a separate process.
So in my application, in the main process it has WCF service (called MainService), then in MainService, it starts another service by creating new Process() and the executable in the children process will use ServiceHost() to create a new WCF service.
When the MainService is stopped by using ServiceHost.Close(), and I use "netstat -a" at commandline and I saw the tcp listener for the MainService port is still alive. However, if I kill all child processes, then it's gone. If I don't kill the child process, and I try to restart the MainService, I will get error:
System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:10100. Make sure that you a
re not trying to use this endpoint multiple times in your application and that there are no other applications listening on this e
ndpoint. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normall
y permitted
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.ServiceModel.Channels.SocketConnectionListener.Listen()
--- End of inner exception stack trace ---
at System.ServiceModel.Channels.SocketConnectionListener.Listen()
at System.ServiceModel.Channels.BufferedConnectionListener.Listen()
at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at IGT.SB.FAC.SCServiceImplementation.ServiceControllerAssistant.OnActivate()5/16/2007 7:12:35 AM: IGT.SB.FAC.Executable.ServiceControllerApplication.Start
Service Controller failed to start.
Error: System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:10100. Make sure that
you are not trying to use this endpoint multiple times in your application and that there are no other applications listening on
this endpoint. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is n
ormally permitted
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at System.ServiceModel.Channels.SocketConnectionListener.Listen()
--- End of inner exception stack trace ---
at System.ServiceModel.Channels.SocketConnectionListener.Listen()
at System.ServiceModel.Channels.BufferedConnectionListener.Listen()
at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.ConnectionOrientedTransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at IGT.SB.FAC.SCServiceImplementation.ServiceControllerAssistant.OnActivate()
at IGT.SB.FAC.SCServiceImplementation.ServiceControllerAssistant.Activate()
at IGT.SB.FAC.SCServiceImplementation.ServiceControllerAssistant.Start()
at IGT.SB.FAC.Executable.ServiceControllerApplication.Start()I don't know what I am doing wrong here. I don't know what I miss to set in WCF. I try to find something in WCF binding that match with SO_reuseAddr in winsock.
In WCF, how can I find out if any channel is already in used? or How can I set the channel listener to reuse the port without using portSharingEnable?
Thank very much in advance for all your help
Anna
Answers
- Once the service host is closed, its associated port should be closed. A process cannot hold a port open if it's not running, so I'm realy unclear as to what's going on there. My best guess is that you have another process holding that port (not MainService, which was shutdown).
All Replies
- What process is holding the port open? What port is the other process (the one you start from MainService) listening on?
So in my application, in the main process it has WCF service (called MainService), then in MainService, it starts another service by creating new Process() and the executable in the children process will use ServiceHost() to create a new WCF service.
As per the error message you're getting and your description, it sounds like you're creating multiple servicehosts on the same endpoint, and this is not allowed. If you're running multiple service hosts in your application you will need to give them seperate and unique endpoints to utilize.
Have you run it in the debugger to verify that each ServiceHost you're running properly closes() without exception? (maybe you're swallowing exceptions somewhere and it kills the service but isn't actually closing the host connection properly).
Where in "MainService" are you instantiating this child servicehost and when are you closing it's connection? Maybe it's never closing the child servicehost connection properly?
Hi Shy Cohen,
Each process has different port.
Main process which is MainService has port (10000) and each other process has port of the 10000+ incremented by 100. For example, ServiceA has port 10100 and serviceA has port 10200 and etc... In the child processes, the servicehost has tcp and http binding, but they all have a dedicated port. I even have tcp port at 10200 and http at 10201.
When I shutdown the MainService and go into the debugger and the ServiceHost in the main was closed. However, the tcp port channel listener of the MainService is still listening. If I shutdown other child processes that was created by the MainService, then tcp port channel listener went away. I checked the status of the port by using netstat -a.
I am thinking some in the child processes hold on the port, but the MainService doesn't communicate with child process using tcp, it uses net.pipe binding to communicate with child process. I also check that there are not from the child processes that hold on to MainService's reference.
Please help!
thank you for your reply.
Anna
Hi JDPeckham,
When I close the MainService, I don't close the child serviceHost since it is in a separate process and I want it to run by it self with shutdown by the MainService when the MainService is down. The child serviceHost was createed in a separate process and not in the main process.
I also went to the debugger to ensure the MainService's serviceHost is closed.
If you need more detail for clarification, please ask me more questions.
Thanks for the reply and concern,
Anna
- Once the service host is closed, its associated port should be closed. A process cannot hold a port open if it's not running, so I'm realy unclear as to what's going on there. My best guess is that you have another process holding that port (not MainService, which was shutdown).
- I got the same error message and mine happens while the application is up and running; it works for a while then the service freezes and that's the error I get in my log; the service is shown as started but it doesn't respond anymore and has to be restarted. What's the most frustrating is that I am unable to reproduce the problem while debugging, it only happens on the production server. The port given in the error is hosted on its own tcp/ip port and no other appliation uses that port.
It's like the application is trying to rehost the same service while it's running but that makes little sense and trying it in debug only prevent the second one from working. I got the same error message and mine happens while the application is up and running; it works for a while then the service freezes and that's the error I get in my log; the service is shown as started but it doesn't respond anymore and has to be restarted. What's the most frustrating is that I am unable to reproduce the problem while debugging, it only happens on the production server. The port given in the error is hosted on its own tcp/ip port and no other appliation uses that port.
It's like the application is trying to rehost the same service while it's running but that makes little sense and trying it in debug only prevent the second one from working.
I am having the same problems in my produciton environment. Did you ever find a solution?
Thanks.If you create a child process it normally inherits most of the handles from the parent process. I know, it sounds strange.
So in this case the parent process opens a socket, then it creates a child process which inherits the socket handle, and then the parent process ends but the child process is still alive holding the inherited socket handle. Then you trie to restart the parent process, but it can not create a socket to the same port because of the inherited handle in the child process.
There are at least two ways to prevent it: specify for each handle that it should not be inheritable, or specify that the child process should not inherit any handles. Use CreateProcess to start the child process with parameter bInheritHandles = false.
A possible third solution, specifically for sockets, is to call setsockopt(SO_REUSEADDR) before you open the listening socket in the restarted the parent process.


