默认 NetTcpBinding

默认示例演示绑定NetTcpBinding的使用。 此示例基于《入门指南》,实现了计算器服务。 在此示例中,该服务是自托管的。 客户端和服务都是控制台应用程序。

注释

本示例的设置过程和生成说明位于本主题末尾。

绑定在客户端和服务配置文件中指定。 绑定类型指定在>元素的属性中,如以下示例配置所示。

<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 示例 执行One-Time 安装过程。

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

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

    注释

    由于服务器是自承载的,因此必须在客户端的 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>