IP端点0.0.0.0:13000上已有一个侦听器。 ?? (TCP使用WCF)

时间:2012-03-16 21:21:35

标签: c# .net windows wcf tcp

我试图弄清楚为什么即使在重新启动计算机后仍在使用该端口!

  

System.ServiceModel.AddressAlreadyInUseException:IP端点0.0.0.0:13000上已有一个侦听器。如果有另一个应用程序已在此端点上侦听,或者如果服务主机中有多个服务端点具有相同的IP端点但具有不兼容的绑定配置,则可能会发生这种情况。 ---> System.Net.Sockets.SocketException:通常只允许使用每个套接字地址(协议/网络地址/端口)          在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)          在System.Net.Sockets.Socket.Bind(EndPoint localEP)          在System.ServiceModel.Channels.SocketConnectionListener.Listen()          ---内部异常堆栈跟踪结束---          在System.ServiceModel.Channels.SocketConnectionListener.Listen()          在System.ServiceModel.Channels.TracingConnectionListener.Listen()          在System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting()          在System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()          在System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)          在System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)          在System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(TimeSpan超时)          在System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)          在System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan超时)          在System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)          在System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan超时)          在System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超时)          在Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)       System.Net.Sockets.SocketException(0x80004005):通常只允许使用每个套接字地址(协议/网络地址/端口)          在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress socketAddress)          在System.Net.Sockets.Socket.Bind(EndPoint localEP)          在System.ServiceModel.Channels.SocketConnectionListener.Listen()

如何确定哪个进程正在侦听该端口(13000)? Netstat在该端口上没有显示任何内容。

这是我的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>

6 个答案:

答案 0 :(得分:27)

我在安装.Net 4.5之后遇到了这个问题,并且我在这里发布了一个解决方案来帮助其他人,如果他们偶然发现它。 @ berkayk上面的答案有效(在不同的端口上暴露mex),但我需要通过同一个端口公开两个端点。

假设您有两个端点,一个使用netTcpBinding,另一个使用mexTcpBinding。 使用默认绑定时,某些默认值是使用OSEnvironmentHelper.ProcessorCount而不是.Net 4.0中的硬编码值计算的。

在我的例子中,当使用命名的netTcpBinding bindingConfiguration时,为MaxConnections属性提供的值为20.在NetTcpBinding上设置MaxConnections属性还将它的TcpTransportBindingElement的MaxPendingConnections属性和TcpTransport的ConnectionPoolSettings.MaxOutboundConnectionsPerEndpoint属性设置为相同的值。

如果不使用命名的netTcpBinding bindingConfiguration,并且仅使用默认值,则使用以下算法计算MaxPendingConnections属性:

 return (12 * OSEnvironmentHelper.ProcessorCount);

mexTcpBinding的传输也使用上面的算法计算了它的MaxPendingConnections属性,所以当两者都没有使用命名的bindingConfiguration时,默认值匹配并且没有问题。

当使用命名的netTcpBinding bindingConfiguration时,传输的MaxPendingConnections为20,并且mexTcpBinding的传输的MaxPendingConnections在我的机器上为96.共享同一端口的这两个端点之间的MaxPendingConnections值的差异是不兼容的。

我还发现此问题也发生在ListenBacklog集中。 (我不知道可能存在的所有可能存在的冲突值。)

要解决此问题,您可以为mex创建与netTcpBinding的命名bindingConfiguration匹配的自定义绑定。示例如下:

<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>

或者,您不能指定任何计算的值(如maxConnections和listenBacklog)并接受默认值(请注意,MaxOutboundConnectionsPerEndpoint仍将保留默认值10,因为它的计算方式与MaxPendingConnections属性的计算方式不同):

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

注意:此问题在此处描述:http://msdn.microsoft.com/en-us/library/aa702636.aspx,但给出的唯一解决方案是在不同的端口上公开mex。下面是反射器中的一些截图,显示了计算MaxPendingConnections默认值时.net 4.0和4.5之间的区别:

.Net 4.0 ConnectionOrientedTransportBindingElement Calculating default MaxPendingConnections

.Net 4.5 ConnectionOrientedTransportBindingElement Calculating default MaxPendingConnections

.Net 4.5 GetMaxPendingConnections method

答案 1 :(得分:14)

安装Visual Studio 2012进行评估后,我遇到了同样的问题。似乎普通服务和mex服务不能共享相同的端口,因为它曾经在.NET 4.0中使用相同的配置文件(我不知道为什么,必须有一个原因)。简而言之,我在不同程序集上的不同程序集和wpf应用程序上有我的服务客户端引用。我用不同的端口发布了mex

<service name="X.XService.XService" behaviorConfiguration="myServiceBehavior">
            <endpoint address="" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IXService" contract="X.XService.IXService">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
            <endpoint address="net.tcp://localhost:9103/XService/mex" binding="mexTcpBinding" bindingConfiguration="" contract="IMetadataExchange" />
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:9102/XService" />
                </baseAddresses>
            </host>
        </service>

我的服务参考程序集配置

<endpoint address="net.tcp://localhost:9103/XService/mex"
            binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IXService"
            contract="XService.IXService" name="NetTcpBinding_IXService">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>

最后我的客户端应用配置

    <endpoint address="net.tcp://localhost:9102/XService" binding="netTcpBinding"
            bindingConfiguration="NetTcpBinding_IXService" contract="XService.IXService"
            name="NetTcpBinding_IXService" behaviorConfiguration="endPointBehavior">
            <identity>
                <dns value="localhost" />
            </identity>
        </endpoint>

答案 2 :(得分:5)

您确定您的服务是唯一一个收听端口13000的服务吗?

在启动程序之前运行netstat -noa | find "13000"以确定哪个进程打开了端口13000。最右侧列中的数字将是进程ID。

然后运行tasklist | find "<pid>",其中是上一个命令的进程ID。这将告诉您哪个进程有13000个打开。

答案 3 :(得分:4)

netsat -anb(需要管理员权限)会告诉你在所有端口上监听什么...如果没有显示任何内容你可能有一个错误,你试图多次创建端点。

答案 4 :(得分:2)

您是否安装了.Net Framework 4.5 beta?我在使用4.5的机器上运行时遇到了同样的错误,而它在4.0上完美运行。但是如果我删除mex端点,它会再次起作用。

在我的情况下,4.5更改中的某些内容导致了相同的错误消息。可能是创建了自动mex端点或其他东西。

没有看到任何其他使用该端口的应用程序,netstat什么都没显示。所以这是某种自我否定......

答案 5 :(得分:-1)

嗯......当我将目标框架更改为.Net framework 4 Client Profile时,我工作了。

尝试一下......希望它也适合你!

相关问题