bema
bema

Reputation: 395

Mono and WCF Net.Tcp Binding on all IP Addresses

As mentioned in this article, in .net it's possible to bind a web service to all ip addresses using the "address" 0. However, this doesn't seem to work with mono (version 2.10.8.1).

Here is my example code:

Client:

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 all works fine.
On my Linux machine (Raspberry Pi, Debian soft-float) the service start without any problems, however the client cant connect.
If I host the service with its ip address instead of the "0" address everything works correct.

Is this just another bug in mono or do i have to bind to any other ip address instead of the 0? If it's a bug in mono, are there any workarounds?

(By the way, I'm still searching for an workaround for the port sharing problems with mono/net.tcp, if anyone could help here -> net.tcp port sharing and mono)

Upvotes: 4

Views: 3870

Answers (3)

George Gutsol
George Gutsol

Reputation: 1

Tutorial to make WCF work through NetTcpBinding on Mono

To use IP address for WCF in Mono u need to write domain name in file /etc/hosts. Then u will be able to use IP address for connecting host and client.

How to write domain: Open file /etc/hosts on host and add IP.address.of.host HostName

Upvotes: 0

Aditya Kresna
Aditya Kresna

Reputation: 1

I had similar problem in Ubuntu Server 16.04 LTS & Mono JIT compiler version 5.0.1.1 (2017-02/5077205 Thu May 25 09:19:18 UTC 2017).

I got it resolved by adding the following line in /etc/hosts:

0.0.0.0 IP4Any

Then ofcourse, bind the service to IP4Any.

Upvotes: 0

PhonicUK
PhonicUK

Reputation: 13864

Try using + or * instead of 0 to bind to all addresses.

Edit: Or try 0.0.0.0

Edit2: Looks like a bug in Mono. You can report it at https://bugzilla.xamarin.com/index.cgi

Upvotes: 0

Related Questions