有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();
}
}
爲什麼在客戶端和服務器上使用不同的IP地址?嘗試將其設置爲127.0.0.1(如果客戶端和服務器在同一臺計算機上)或192.168.x.y.如果這樣做沒有幫助,那麼NetTcp也可能在Linux上有問題,下週我會看看。 –
因爲客戶端anf服務器ara不同的機器, –
我使用不同的IP成功通過基本的http,和其他人在stackoverflaw成功通過nettcp,但他們的問題是不同於我 –