As mentioned in this article, in .net you can associate a web service with all IP addresses using the "address" 0. However, this does not seem to work with mono (version 2.10.8.1).
Here is my sample code:
Customer:
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(); } } }
If I run this (service and client) on a Windows PC using .net, everything works fine.
On my Linux machine (Raspberry Pi, Debian soft-float) the service starts without any problems, however, the client cannot connect.
If I host a service with its IP address instead of the "0" address, everything works correctly.
Is this just another mistake in mono or do I need to bind to any other ip address instead of 0? If this is a bug in mono, are there any workarounds?
(By the way, I'm still looking for a workaround for port-sharing issues with mono / net.tcp, if someone can help here - > net.tcp and mono file sharing )