Frage

There

Context: I write a demo console app -a WCF service which running on Linux by mono, and a console Client which running on Windows 7.

Linux Versoin: Both on Ubuntu and Match-box

Mono Version: 2.10.8.1(On Ubuntu) and 2.10.6(On Match-box)

Issue: The client could communicate with Service by basicHttpBinding but NOT netTcpBinding.

Exception Info: Could not connect to net.tcp://192.168.1.220/. The connection attempt lasted for a time span of 00:00:14.0408031. TCP error code 10060: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 192.168.1.220:808.

Service Code:

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();
        }
    }
}

Service app.config(partial, here i just list the endpoints and bindings)

<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> 

Client Code:

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();
    }
}
War es hilfreich?

Lösung

You need to use the IP address of your host machine that's publicly visible on the network if you want to connect to it remotely using net.tcp.

I just tested this on Windows and net.tcp and http actually behave differently when using 127.0.0.1: while HTTP always seem to listen on all interfaces regardless of which IP address you use on your endpoint, using a specific IP such as 127.0.0.1 makes NetTCP only listen on that particular address.

However, you can use net.tcp://localhost:<port>/<path> to make it listen on all interfaces (this was was implemented in Mono 2.10.10, see bug #275).

So either use net-tcp://localhost:808/ in your app.config to listen on all interfaces or explicitly set it to a specific IP address (that's publicly visible on the network) using net-tcp://192.168.1.200:808/ (or whatever the machine's IP address is).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top