2012-11-30 39 views
1
溝通

WCF服務在Linux上無法與客戶端在Windows上通過NetTcp

語境: 我寫它通過單在Linux上運行演示控制檯應用程序-a WCF服務,以及上運行的控制檯客戶端Windows 7的

Linux的Versoin:無論在Ubuntu和火柴盒

單聲道版本:2.10.8.1(在Ubuntu)和2.10.6(在火柴盒)

期: 客戶端可以通過basicHttpBinding 與服務進行通信,但不是 netTcpBinding。

異常信息: 無法連接到net.tcp://192.168.1.220/。連接嘗試持續時間爲00:00:14.0408031。 TCP錯誤代碼10060:連接嘗試失敗,因爲連接方在一段時間後未正確響應,或者由於連接的主機未能響應192.168.1.220:808而導致建立的連接失敗。

服務代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     try 
     { 
      ServiceHost sh = new ServiceHost(typeof(DynIPService.DynIPService)); 
      //sh.AddServiceEndpoint("DynIPServiceContract.IDynIPService", binding, "net.tcp://10.161.66.213:808"); 
      sh.Open(); 
      foreach (var ep in sh.Description.Endpoints) 
      { 
       Console.WriteLine("Address: {0}, ListenUri: {1}, ListenUriMode: {2} ", ep.Address, ep.ListenUri, ep.ListenUriMode); 
      } 
      Console.WriteLine("Service is running");    
     } 
     catch (Exception ex) 
     { 
      Console.WriteLine("Error:" + ex.Message); 
      throw; 
     } 
     finally 
     { 
      Console.ReadKey(); 
     } 
    } 
} 

服務的app.config(部分,在這裏我只列出端點和綁定)

<services> 
    <service name="DynIPService.DynIPService" behaviorConfiguration="MyServiceTypeBehaviors" > 
    <endpoint address="net.tcp://127.0.0.1:808" 
       binding="netTcpBinding" bindingConfiguration="TCP_Binding" 
       contract="DynIPServiceContract.IDynIPService"/> 
    <endpoint address="http://127.0.0.1:123" 
       binding="basicHttpBinding" bindingConfiguration="HTTP_Binding" 
       contract="DynIPServiceContract.IDynIPService"/> 
    </service> 
</services> 
<bindings> 
    <basicHttpBinding> 
    <binding name="HTTP_Binding"> 
     <security mode="None"></security> 
    </binding> 
    </basicHttpBinding> 
    <netTcpBinding> 
    <binding name="TCP_Binding"> 
     <security mode="None"></security> 
    </binding> 
    </netTcpBinding> 
</bindings> 

客戶端代碼:

class Program 
{ 
    static void Main(string[] args) 
    { 
     //var ip = "localhost.localdomain"; 
     var ip = "192.168.1.220"; 

     //Tcp 
     Binding tcpBinding = new NetTcpBinding(SecurityMode.None); 
     var tcpUri = new Uri(string.Format("net.tcp://{0}:808", ip)); 

     //http 
     Binding httpBinding = new BasicHttpBinding(); 
     var httpUri = new Uri(string.Format("http://{0}:123",ip)); 

     EndpointAddress address = new EndpointAddress(httpUri.ToString()); 
     IDynIPService proxy = ChannelFactory<IDynIPService>.CreateChannel(httpBinding, address); 
     var hostIP = proxy.ReadHostIp(); 
     Console.WriteLine(hostIP); 
     Console.ReadKey(); 
    } 
} 
+0

爲什麼在客戶端和服務器上使用不同的IP地址?嘗試將其設置爲127.0.0.1(如果客戶端和服務器在同一臺計算機上)或192.168.x.y.如果這樣做沒有幫助,那麼NetTcp也可能在Linux上有問題,下週我會看看。 –

+0

因爲客戶端anf服務器ara不同的機器, –

+0

我使用不同的IP成功通過基本的http,和其他人在stackoverflaw成功通過nettcp,但他們的問題是不同於我 –

回答

1

如果要使用net.tcp遠程連接它,則需要使用網絡上公開可見的主機的IP地址。

我剛剛在Windows上測試了這個,net.tcp和http實際上在使用127.0.0.1時的行爲有所不同:儘管HTTP總是在所有接口上監聽,而不管您在端點上使用哪個IP地址,使用特定的IP,如127.0.0.1使NetTCP只能監聽該特定地址。

但是,您可以使用net.tcp://localhost:<port>/<path>讓它偵聽所有接口(這是在單2.10.10落實,見bug #275)。

因此,無論使用net-tcp://localhost:808/app.config監聽所有接口或明確地將其設置爲一個特定的IP地址使用(這是公開可見的網絡上)net-tcp://192.168.1.200:808/(或任何本機的IP地址)。

+0

Martin,一旦我在app.config中使用Endpoint的地址在網絡上公開顯示的機器IP,它總是提示:「沒有這樣的主機是已知的」,但是當我添加一條記錄時 –

+0

192.168.1.220 xxxxxxxx到/ etc/hosts文件。它確實有效!我認爲這是在您之前提到的錯誤報告中修復的錯誤。但我想知道哪個版本的單聲道修復bug.thanks –

+0

另一個嘗試:我將單聲道版本從2.10.6升級到2.10.8.1。然後將app.config中的IP修改爲當前機器IP(192.168.1.220),不需要更改/ etc/hosts文件。它可以工作。但將app.config中的IP修改爲localhost,監聽URL爲127.0.0.1,因此它不起作用。 –

相關問題