C# – There is already a listener on IP endpoint 0.0.0.0:13000. ?? (TCP using WCF)

cnettcpwcfwindows

I'm trying to figure out why the port is being used even after restarting the computer!

System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:13000. This could happen if there is another application already listening on this endpoint or if you have multiple service endpoints in your service host with the same IP endpoint but with incompatible binding configurations. —> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally 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.TracingConnectionListener.Listen()
at System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting()
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.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 Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)
System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally 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()

How do you figure out which process is listening to that port (13000)? Netstat shows nothing on that port.

Here's my App.config:

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="SomeTarget.SomeTargetService">
        <endpoint address="" binding="customBinding" bindingConfiguration="NetTcpBinding"
          contract="SomeTarget.ISomeTargetService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
          contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="net.tcp://localhost:13000" />
          </baseAddresses>
        </host>
      </service>
    </services>

    <bindings>
      <customBinding>
        <binding name="NetTcpBinding" sendTimeout="00:05:00" closeTimeout="00:00:30" openTimeout="00:00:30" receiveTimeout="00:05:00">
          <transactionFlow />
          <binaryMessageEncoding />
          <windowsStreamSecurity protectionLevel="None" />
          <tcpTransport maxBufferPoolSize="524288"
                        maxReceivedMessageSize="1024"
                        maxBufferSize="1024" >
            <connectionPoolSettings groupName="default" leaseTimeout="00:05:00"
                                    idleTimeout="00:02:00" maxOutboundConnectionsPerEndpoint="20" />
          </tcpTransport>
        </binding>
      </customBinding>
    </bindings>

    <behaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>

Best Answer

I experienced this issue after installing .Net 4.5 and am posting a solution here to help others if they stumble into it. @berkayk's answer above worked (exposing the mex on a different port), but I needed to expose both endpoints through the same port.

Assume you have two endpoints, one using netTcpBinding and one using mexTcpBinding. When using the default bindings, some of the defaults are calculated using OSEnvironmentHelper.ProcessorCount instead of hard coded values as they were in .Net 4.0.

In my case, when using a named netTcpBinding bindingConfiguration, the value supplied for the MaxConnections property was 20. Setting the MaxConnections property on the NetTcpBinding also sets it's TcpTransportBindingElement's MaxPendingConnections property and the TcpTransport's ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint property to the same value.

When not using a named netTcpBinding bindingConfiguration, and only using the default, the MaxPendingConnections property was calculated by using the following algorithm:

 return (12 * OSEnvironmentHelper.ProcessorCount);

The mexTcpBinding's transport also calculated it's MaxPendingConnections property using the above algorithm, so when neither is using a named bindingConfiguration the default values match and there is no issue.

When using a named netTcpBinding bindingConfiguration, the transport's MaxPendingConnections was 20, and the mexTcpBinding's transport's MaxPendingConnections was, on my machine, 96. The difference in values for the MaxPendingConnections between these two endpoints sharing the same port is incompatible.

I also found that this issue occurred with the ListenBacklog set as well. (I do not know of all the possible conflicting values that may exist.)

To resolve the issue, you can create a custom binding for mex that matches the named bindingConfiguration for netTcpBinding. Example below:

<endpoint binding="netTcpBinding" bindingConfiguration="TestNetTcpBinding"
      contract="YourContract" />
<endpoint address="mex" binding="customBinding" bindingConfiguration="TestMexBinding"
      contract="IMetadataExchange" />

<bindings>
<customBinding>
<binding name="TestMexBinding">
<tcpTransport maxPendingConnections="20" listenBacklog="20">                   
<connectionPoolSettings groupName="default"  maxOutboundConnectionsPerEndpoint="20" />
</tcpTransport>
</binding>
</customBinding>
<netTcpBinding>
<binding name="TestNetTcpBinding" listenBacklog="20" maxConnections="20"/>
</netTcpBinding>
</bindings>

Or, you can not specify any values that are calculated (like maxConnections and listenBacklog) and accept the defaults (note that MaxOutboundConnectionsPerEndpoint will still retain the default value of 10 as it is not calculated in the same way that the MaxPendingConnections property is):

<binding name="TestNetTcpBinding" ...someOtherProperties except listenBacklog and maxConnections/>

Note: The issue is described here: http://msdn.microsoft.com/en-us/library/aa702636.aspx, but the only solution given is to expose the mex on a different port. Below are some screenshots in reflector that show the difference between .net 4.0 and 4.5 when calculating the MaxPendingConnections defaults:

.Net 4.0 ConnectionOrientedTransportBindingElement Calculating default MaxPendingConnections

.Net 4.5 ConnectionOrientedTransportBindingElement Calculating default MaxPendingConnections

.Net 4.5 GetMaxPendingConnections method

Related Topic