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"
  <endpoint address=""
  <dns value="localhost" />
  <endpoint address="mex"
      contract="IMetadataExchange" />
        <add baseAddress="net.tcp://localhost:8181/MathService" />

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