正如this文章中所述,在.net中,可以使用「地址」0將Web服務綁定到所有IP地址。但是,這似乎不起作用與單聲道(版本2.10.8.1)。所有IP地址上的Mono和WCF Net.Tcp綁定
這裏是我的示例代碼:
客戶:
string ipAddressOfTheService = "192.168.0.23";
EndpointAddress address = new EndpointAddress(string.Format("net.tcp://{0}:8081/myService", ipAddressOfTheService));
NetTcpBinding binding = new NetTcpBinding();
ServiceProxy proxy = new ServiceProxy(binding, address);
if(proxy.CheckConnection())
{
MessageBox.Show("Service is available");
}
else
{
MessageBox.Show("Service is not available");
}
ServiceProxy:
public class ServiceProxy : ClientBase<IMyService>, IMyService
{
public ServiceProxy(Binding binding, EndpointAddress address)
: base(binding, address)
{
}
public bool CheckConnection()
{
bool isConnected = false;
try
{
isConnected = Channel.CheckConnection();
}
catch (Exception)
{
}
return isConnected;
}
}
IMyService:
[ServiceContract]
public interface IMyService
{
[OperationContract]
bool CheckConnection();
}
爲MyService:
class MyService : IMyService
{
public bool CheckConnection()
{
Console.WriteLine("Check requested!");
return true;
}
}
的ServiceHost:
class MyServiceHost
{
static void Main(string[] args)
{
Uri baseAddress = new Uri(string.Format("net.tcp://0:8081/myService");
using (ServiceHost host = new ServiceHost(typeof(MonitoringService), baseAddress))
{
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.None;
host.AddServiceEndpoint(typeof(IMyService), binding, baseAddress);
host.Open();
Console.WriteLine("The service is ready at {0}", baseAddress);
Console.WriteLine("Press <Enter> to stop the service.");
Console.ReadLine();
host.Close();
}
}
}
如果我使用.NET運行在Windows PC上這(服務端和客戶端),一切工作正常。
在我的Linux機器(Raspberry Pi,Debian soft-float)上,服務啓動沒有任何問題,但客戶端無法連接。
如果我使用其IP地址而不是「0」地址託管服務,那麼一切正常。
這只是單聲道的另一個錯誤,還是我必須綁定到任何其他IP地址而不是0? 如果它是單聲道中的錯誤,是否有任何解決方法?
(順便說一句,我還在尋找的端口共享問題單/的net.tcp的解決辦法,如果任何人都可以在這裏幫助 - >net.tcp port sharing and mono)
使用*而不是0我得到: '未處理的異常:System.Net.Sockets.SocketException:沒有這樣的主機是已知的',With + I get:'未處理的異常:System.UriFormatException:無效的URI:主機名無法解析。' – bema
請嘗試使用'0.0.0.0'。 – PhonicUK
與0.0.0.0我得到:'未處理的異常:System.ArgumentException:地址0.0.0.0(IPv4)和:: 0(IPv6)是未指定的地址。你不能用它們作爲目標地址。「謝謝你的快速回答:) – bema