默认 NetTcpBinding

本示例演示 NetTcpBinding 绑定的用法。此示例基于实现计算器服务的入门示例。在本示例中,服务是自承载服务。客户端和服务都是控制台应用程序。

ms752250.note(zh-cn,VS.100).gif注意:
本主题的最后提供了此示例的设置过程和生成说明。

ms752250.Important(zh-cn,VS.100).gif 注意:
您的计算机上可能已安装这些示例。在继续操作之前,请先检查以下(默认)目录。

<安装驱动器>:\WF_WCF_Samples

如果此目录不存在,请访问针对 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例(可能为英文网页),下载所有 Windows Communication Foundation (WCF) 和 WF 示例。此示例位于以下目录。

<安装驱动器>:\WF_WCF_Samples\WCF\Basic\Binding\Net\TCP\Default

绑定是在客户端和服务的配置文件中指定的。绑定类型是在 <endpoint> 元素的 binding 特性中指定的,如下面的示例配置所示。

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

前面的示例演示如何配置终结点以使用具有默认设置的 netTcpBinding 绑定。如果要配置 netTcpBinding 绑定并更改它的一些设置,则必须定义绑定配置。终结点必须使用 bindingConfiguration 属性按名称来引用绑定配置。在本示例中,绑定配置的名称为 Binding1 并按下面的示例配置所示进行定义。

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

运行示例时,操作请求和响应将显示在客户端控制台窗口中。在客户端窗口中按 Enter 以关闭客户端。

    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.

设置、生成和运行示例

  1. 使用以下命令安装 ASP.NET 4.0。

    %windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable
    
  2. 请确保已执行 Windows Communication Foundation 示例的一次性安装过程

  3. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  4. 若要用单机配置或跨计算机配置来运行示例,请按照Running the Windows Communication Foundation Samples中的说明进行操作。

    ms752250.note(zh-cn,VS.100).gif注意:
    由于该服务器是自承载的,因此必须在客户端的 App.config 文件中指定一个标识才能用跨计算机配置来运行该示例。

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