netTcpBinding is a secure, reliable binding suitable for cross-machine communication. It uses TCP for message delivery along with a binary message encoding which makes is faster than HTTP based bindings such as WSHttpBinding and BasicHttpBinding.

Hosting a netTcpBinding service in IIS 7 is simple, however it requires several steps.


1. Add Non-HTTP Activation: from the Add Features Wizard select .NET Framework 3.0 Features and add Non-HTTP Activation




2. Make sure the Net.TCP services are running:
netTcpBinding uses two windows services. NetTcpActivator - Receives activation requests over the net.tcp protocol and passes them to the Windows Process Activation Service and NetTcpPortSharing - provides ability to share TCP ports over the net.tcp protocol. This service enables net.tcp ports to be shared across multiple user processes which will make it easier to manage open ports in your network.



3. In IIS navigate to your website and select Advanced Settings from the right menu. Add net.tcp to the Enabled Protocols.




4. In IIS select Bindings from the right menu. Add a new net.tcp binding with the port you wish to use. I used port 808 so my Binding information looks like 808:*






5. From now on the system is ready to run netTcpBinding so you can now add it to web.config:


<system.serviceModel>
      <services>
            <service behaviorConfiguration="MyBehavior"
            name="MyNamespace.MyService">
            <endpoint address=""
                          binding="netTcpBinding"
                          bindingConfiguration="portSharingBinding"
                          name="MyServiceEndpoint"
                          contract="MyNamespace.IMyService">
                  <identity>
                        <dns value="localhost" />
                  </identity>
            </endpoint>
 
            <endpoint address="mex"
                          binding="mexTcpBinding"
                          bindingConfiguration=""
                          name="MyServiceMexTcpBidingEndpoint"
                          contract="IMetadataExchange" />
                  <host>
                        <baseAddresses>
                              <add baseAddress="net.tcp://MyServerIPAddress:808/MyService.svc" />
                        </baseAddresses>
                  </host>
            </service>
      </services>
      <behaviors>
        <serviceBehaviors>
            <behavior name="MyBehavior">
              <serviceMetadata httpGetEnabled="false" />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
      </behaviors>
      <bindings>
            <netTcpBinding>
                  <binding name="portSharingBinding" portSharingEnabled="true"/>
            </netTcpBinding>
      </bindings>
</system.serviceModel>


A mexTcpBinding endpoint is needed in order to use IMetadataExchange over TCP. IMetadataExchange exposes methods used to return metadata about a service. Now the service should be up and running so from Visual Studio just add a Service reference. Point the reference to:
net.tcp://MyServerIPAddress:808/MyService.svc. It should generate the following configuration

<system.serviceModel>
          <bindings>
                    <netTcpBinding>
                             <binding name="MyServiceEndpoint" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" transactionFlow="false" transferMode="Buffered" transactionProtocol="OleTransactions" hostNameComparisonMode="StrongWildcard" listenBacklog="10" maxBufferPoolSize="524288" maxBufferSize="65536" maxConnections="10" maxReceivedMessageSize="65536">
                                      <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                                      <reliableSession ordered="true" inactivityTimeout="00:10:00" enabled="false" />
                                      <security mode="Transport">
                                               <transport clientCredentialType="Windows" protectionLevel="EncryptAndSign" />
                                               <message clientCredentialType="Windows" />
                                      </security>
                             </binding>
                    </netTcpBinding>
          </bindings>
          <client>
                    <endpoint address="net.tcp://MyServerName/MyService.svc" binding="netTcpBinding" bindingConfiguration="MyServiceEndpoint" contract="MyNamespace.IMyService" name="MyServiceEndpoint">
                              <identity>
                                        <dns value="localhost" />
                              </identity>
                    </endpoint>
          </client>
</system.serviceModel>


Now you are ready to use the service from the client you generated:


using (MyServiceClient client = new MyServiceClient())
{
        client.Open();
        client.MyMethod();
        client.Close();
}


That’s it. You should be able to communicate with your service faster and more efficiently than any HTTP based binding