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

[5359 byte] By [ChannelListener] at [2008-1-5]
# 1
What process is holding the port open? What port is the other process (the one you start from MainService) listening on?
ShyCohen-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 2

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?

JDPeckham at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 3

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

ChannelListener at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 4

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

ChannelListener at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...
# 5
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).
ShyCohen-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Orcas,Windows Communication Foundation (Indigo)...

Visual Studio Orcas

Site Classified