WCF netTcpBinding Through a Router

Today at lunch, we got on the subject of the WCF netTcpBinding and whether or not it was able to traverse routers (and even NAT).  Highly nerdy lunch, admittedly.

So my co-worker Matthew and I decided to spend our RECESS this afternoon proving it out.netTcpBindingMachines_0A851B1E

Here’s a picture of what we setup.  A WCF service sitting out on the internet, and a client using WCF to call the service.  The call goes out through a router that is doing NAT on the internal IP address of the client (192.168.1.*) and sending the message out to the service.

 

Service config:

<service name="NetTcpBindingTest.MathService"
      behaviorConfiguration="DefaultTcpBehavior">
  <endpoint address=""
      binding="netTcpBinding"
      bindingConfiguration="netTcpBuffered"
      name="netTcp"
      contract="Interfaces.IMath">
  <identity>
  <dns value="localhost" />
    </identity>
  </endpoint>
  <endpoint address="mex"
      binding="mexTcpBinding"
      bindingConfiguration="mexTcp"
      name="mexTcp"
      contract="IMetadataExchange" />
  <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8181/MathService" />
      </baseAddresses>
  </host>
</service>

Client ChannelFactory:

var math = ChannelFactory<Interfaces.IMath>.CreateChannel(
    new NetTcpBinding( SecurityMode.None ),
    new EndpointAddress( @"net.tcp://70.50.y.y:8181/MathService" ) );

Success!  The netTcpBinding definitely makes it through the NAT router.

We weren’t done yet – next we added a callback interface into the mix, so the service could make out-of-band / unsolicited calls back to the client on the duplex channel binding.  A bit surprising that this actually worked as well.

 

WCF Interfaces:

[ServiceContract( CallbackContract = typeof( IMathCallback ) )]
public interface IMath 
{ 
    [OperationContract]    
    int Add( int x, int y );
}
    
    public interface IMathCallback 
{ 
    [OperationContract( IsOneWay = true )]    
    void AnotherAnswer( int z );
}

 

Client DuplexChannelFactory:

var math = DuplexChannelFactory<Interfaces.IMath>.CreateChannel(
    new InstanceContext( this ),
    new NetTcpBinding( SecurityMode.None ), 
    new EndpointAddress( @"net.tcp://70.50.y.y:8181/MathService" ) );

 

Moral of the story:  netTcpBinding is router and NAT friendly.  (MSDN docs actually say that it will work with some NAT routers, and there’s a TON of discussions about this on the internet, so your mileage may vary)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>