Default NetTcpBinding

 

This sample demonstrates the use of the NetTcpBinding binding. This sample is based on the Getting Started Sample that implements a calculator service. In this sample, the service is self-hosted. Both the client and service are console applications.

System_CAPS_noteNote

The setup procedure and build instructions for this sample are located at the end of this topic.

System_CAPS_importantImportant

The samples may already be installed on your machine. Check for the following (default) directory before continuing.

<InstallDrive>:\WF_WCF_Samples

If this directory does not exist, go to Windows Communication Foundation (WCF) and Windows Workflow Foundation (WF) Samples for .NET Framework 4 to download all Windows Communication Foundation (WCF) and WF samples. This sample is located in the following directory.

<InstallDrive>:\WF_WCF_Samples\WCF\Basic\Binding\Net\TCP\Default

The binding is specified in the configuration files for the client and service. The binding type is specified in the binding attribute of the 13aa23b7-2f08-4add-8dbf-a99f8127c017 element as shown in the following sample configuration.

<endpoint address=""
          binding="netTcpBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />

The previous sample shows how to configure an endpoint to use the netTcpBinding binding with the default settings. If you want to configure the netTcpBinding binding and change some of its settings, it is necessary to define a binding configuration. The endpoint must reference the binding configuration by name with a bindingConfiguration attribute. In this sample, the binding configuration is named Binding1 and is defined as shown in the following sample configuration.

<services>
  <service name="Microsoft.ServiceModel.Samples.CalculatorService"
           behaviorConfiguration="CalculatorServiceBehavior">
    ...
    <endpoint address=""
              binding="netTcpBinding"
              bindingConfiguration="Binding1" 
              contract="Microsoft.ServiceModel.Samples.ICalculator" />
    ...
  </service>
</services>

<bindings>
  <netTcpBinding>
    <binding name="Binding1" 
             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" />
      </security>
    </binding>
  </netTcpBinding>
</bindings>

When you run the sample, the operation requests and responses are displayed in the client console window. Press ENTER in the client window to shut down the client.

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press ENTER to terminate client.

To set up, build, and run the sample

  1. Install ASP.NET 4.0 using the following command.

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
    
  2. To build the C# or Visual Basic .NET edition of the solution, follow the instructions in Building the Windows Communication Foundation Samples.

  3. To run the sample in a single- or cross-machine configuration, follow the instructions in Running the Windows Communication Foundation Samples.

    System_CAPS_noteNote

    Because the server is self-hosted, you must specify an identity in the client's App.config file to run the sample in a cross-machine configuration.

    <client>
      <endpoint name=""
          address="net.tcp://servername:9000/servicemodelsamples/service" 
          binding="netTcpBinding" 
          contract="Microsoft.ServiceModel.Samples.ICalculator">
            <identity>
              <userPrincipalName value = "user_name@service_domain"/>
            </identity>
      </endpoint>
    </client>
    
Show: