2012-03-16 29 views
15

我試圖弄清楚爲什麼即使在重新啓動計算機後端口仍在使用!IP端點0.0.0.0:13000已經有一個監聽器。 ?? (使用WCF的TCP)

System.ServiceModel.AddressAlreadyInUseException:在IP端點0.0.0.0:13000上已經有一個偵聽器。如果另一個應用程序已在此端點上偵聽,或者您的服務主機中有多個具有相同IP端點但具有不兼容綁定配置的服務端點,則可能會發生這種情況。 ---> System.Net.Sockets.SocketException:通常只允許使用每個套接字地址(協議/網絡地址/端口) 在System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress套接字地址) System.Net.Sockets.Socket.Bind(EndPoint localEP) at System.ServiceModel.Channels.SocketConnectionListener.Listen() ---內部異常堆棧跟蹤結束--- at System.ServiceModel.Channels.SocketConnectionListener.Listen () 在System.ServiceModel.Channels.TracingConnectionListener.Listen() 在System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting() 在System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen() 在System.ServiceModel.Channels.Trans portManager.Open(TransportChannelListener channelListener) 在System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback) 在System.ServiceModel.Channels.TcpChannelListener`2.OnOpen(時間跨度超時) 在System.ServiceModel.Channels.CommunicationObject.Open (時間跨度超時) 在System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(時間跨度超時) 在System.ServiceModel.Channels.CommunicationObject.Open(時間跨度超時) 在System.ServiceModel.ServiceHostBase.OnOpen(時間跨度超時) 在系統.ServiceModel.Channels.CommunicationObject.Open(TimeSpan超時) at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info) System.Net.Sockets.SocketException(0x80004005):通常只允許使用每個套接字地址(協議/網絡地址/端口) 位於System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot,SocketAddress套接字地址) 在System.ServiceModel.Channels.SocketConnectionListener.Listen System.Net.Sockets.Socket.Bind(端點localEP) ()

你如何找出哪個進程監聽端口(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> 
+1

防火牆問題? – 2012-03-16 21:25:19

+0

防火牆不應該給你一個AddressAlreadyInUseException。 – Yaur 2012-03-16 21:28:39

+0

什麼都沒發現... – 2012-03-16 21:44:26

回答

4

netsat -anb(需要管理員權限)會告訴你什麼是對所有端口上偵聽......如果這說明不了什麼,你可能有,你要創建一個bug端點不止一次。

+0

nope,這沒有顯示任何結果 – 2012-03-16 22:23:22

+1

然後設置VS studio以打破所有異常,並且當您看到正在使用的地址異常時,重新運行該命令。 – Yaur 2012-03-16 22:47:58

5

你確定你的服務是唯一一個聽13000端口的嗎?

運行netstat -noa | find "13000"在開始您的程序之前確定哪個進程已打開端口13000。最右邊的列中的數字將是進程ID。

然後運行tasklist | find "<pid>"其中是上一個命令的進程ID。這將告訴你哪個進程打開13000。

+0

什麼都沒發現... – 2012-03-16 21:44:31

2

您是否安裝了.NET Framework 4.5 beta?我在4.5上運行的機器上看到了同樣的錯誤,而它在4.0上運行得很好。但是,如果我刪除mex端點,它會再次運行。

在我的情況下,4.5更改中的某些內容導致了相同的錯誤消息。也許一個自動mex端點被創建或者其他東西。

沒有看到任何其他應用程序使用該端口,netstat什麼也沒有顯示。所以這是某種自我否定...

+0

現在我遇到了這個問題,你有沒有找到解決辦法? – Vasea 2012-05-30 13:06:11

+0

只有在app.config中刪除元數據端點配置纔有幫助。 – 2012-06-01 15:27:58

+0

我在.net 4.5中遇到了問題 - 請參閱我的帖子以獲取解決方案。 – dugas 2012-09-13 18:49:57

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

爲我工作。謝謝 – sura2k 2013-11-19 11:07:11

24

我遇到過這個問題在安裝.NET 4.5後我在這裏發佈一個解決方案來幫助其他人,如果他們絆倒它。 @ berkayk的回答如上(將mex暴露在不同的端口上),但我需要通過相同的端口公開兩個端點。

假設您有兩個端點,一個使用netTcpBinding,另一個使用mexTcpBinding。 使用默認綁定時,某些默認值使用OSEnvironmentHelper.ProcessorCount進行計算,而不是像.NET 4.0中的硬編碼值。

在我的情況下,使用一個名爲NetTcpBinding的bindingConfiguration,爲MaxConnections最大屬性提供值時爲20設置MaxConnections最大財產上的NetTcpBinding的也將是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。以下是在反射一些截圖,顯示.NET 4.0和4.5之間的差額計算MaxPendingConnections默認值時:

.Net 4.0 ConnectionOrientedTransportBindingElement Calculating default MaxPendingConnections

.Net 4.5 ConnectionOrientedTransportBindingElement Calculating default MaxPendingConnections

.Net 4.5 GetMaxPendingConnections method

+5

你是一個拯救生命的人!花了2天的時間,但不能得到它的工作。剛剛從'binding'標記中刪除了'listenBacklog'和'maxConnections',現在它工作的很好!非常感謝! – 1nfected 2012-11-06 07:58:40

+1

它也幫助了我,謝謝! – Francisco 2013-02-12 00:04:31

+1

保存我的一天! – Gandarez 2016-04-29 12:07:56

-1

嗯...我的工作時,我改變了目標框架轉換爲.Net框架4客戶端配置文件。

試試吧......希望它也適用於你!