TCP 激活

TCPActivation 示例演示如何托管使用 Windows 进程激活服务(WAS)来激活通过 net.tcp 协议进行通信的服务。 此示例基于入门指南

注释

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

此示例由客户端控制台程序(.exe)和服务库(.dll)组成,这些库托管在 WAS 激活的辅助进程中。 客户端活动在控制台窗口中可见。

该服务实现定义请求-回复通信模式的协定。 协定由 ICalculator 接口定义,该接口公开数学运算(Add、Subtract、Multiply 和 Divide),如以下示例代码所示:

[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
}

服务实现计算并返回相应的结果:

// Service class that implements the service contract.
public class CalculatorService : ICalculator
{
    public double Add(double n1, double n2)
    {
        return n1 + n2;
    }
    public double Subtract(double n1, double n2)
    {
        return n1 - n2;
    }
    public double Multiply(double n1, double n2)
    {
        return n1 * n2;
    }
    public double Divide(double n1, double n2)
    {
        return n1 / n2;
    }
}

此示例使用启用了 TCP 端口共享且关闭了安全功能的 net.tcp 绑定变体。 如果要使用安全的 TCP 绑定,请将服务器的安全模式更改为所需的设置,并在客户端上重新运行 Svcutil.exe 以生成更新客户端配置文件。

以下示例显示了服务的配置:

<system.serviceModel>

    <services>
      <service name="Microsoft.ServiceModel.Samples.CalculatorService"
               behaviorConfiguration="CalculatorServiceBehavior">
        <!-- This endpoint is exposed at the base address provided by host: net.tcp://localhost/servicemodelsamples/service.svc  -->
        <endpoint binding="netTcpBinding" bindingConfiguration="PortSharingBinding"
          contract="Microsoft.ServiceModel.Samples.ICalculator" />
        <!-- the mex endpoint is exposed at net.tcp://localhost/servicemodelsamples/service.svc/mex -->
        <endpoint address="mex"
                  binding="mexTcpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="PortSharingBinding" portSharingEnabled="true">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>

    <!--For debugging purposes set the includeExceptionDetailInFaults attribute to true-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata />
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

  </system.serviceModel>

客户端的终结点配置如下示例代码所示:

<system.serviceModel>
    <bindings>
        <netTcpBinding>
          <binding name="NetTcpBinding_ICalculator">
            <security mode="None"/>
          </binding>
        </netTcpBinding>
    </bindings>
    <client>
        <endpoint address="net.tcp://localhost/servicemodelsamples/service.svc"
            binding="netTcpBinding" bindingConfiguration="NetTcpBinding_ICalculator"
            contract="Microsoft.ServiceModel.Samples.ICalculator" name="NetTcpBinding_ICalculator" />
    </client>
</system.serviceModel>

运行示例时,操作请求和响应将显示在客户端控制台窗口中。 在客户端窗口中按 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. 确保已安装 IIS 7.0。 WAS 激活需要 IIS 7.0。

  2. 请确保您已经为 Windows Communication Foundation 示例执行了 One-Time 设置程序

    此外,必须安装 WCF 非 HTTP 激活组件:

    1. “开始” 菜单中,选择 “控制面板”。

    2. 选择 “程序和功能”。

    3. 单击“ 打开或关闭 Windows 组件”。

    4. 展开 Microsoft .NET Framework 3.0 节点,并检查 Windows Communication Foundation 非 HTTP 激活功能

  3. 配置 WAS 以支持 TCP 激活。

    为方便起见,以下两个步骤在示例目录中名为AddNetTcpSiteBinding.cmd的批处理文件中实现。

    1. 若要支持 net.tcp 激活,必须先将默认网站绑定到 net.tcp 端口。 可以使用随 Internet Information Services 7.0 (IIS) 管理工具集一起安装的 Appcmd.exe来完成此作。 在管理员级命令提示符下运行以下命令:

      %windir%\system32\inetsrv\appcmd.exe set site "Default Web Site" -+bindings.[protocol='net.tcp',bindingInformation='808:*']
      

      小窍门

      此命令是单行文本。 此命令将 net.tcp 站点绑定添加到默认网站上,使用任意主机名侦听 TCP 端口 808。

    2. 尽管站点中的所有应用程序共享一个通用的 net.tcp 绑定,但每个应用程序可以单独启用 net.tcp 支持。 若要为 /servicemodelsamples 应用程序启用 net.tcp,请从管理员级命令提示符运行以下命令:

      %windir%\system32\inetsrv\appcmd.exe set app
      "Default Web Site/servicemodelsamples" /enabledProtocols:http,net.tcp
      

      注释

      此命令是单行文本。 此命令允许通过http://localhost/servicemodelsamplesnet.tcp://localhost/servicemodelsamples两种方式访问 /servicemodelsamples 应用程序。

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

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

    删除为此示例添加的 net.tcp 站点绑定。

    为方便起见,以下两个步骤在示例目录中名为RemoveNetTcpSiteBinding.cmd的批处理文件中实现。

    1. 通过从管理员级命令提示符运行以下命令,从启用的协议列表中删除 net.tcp:

      %windir%\system32\inetsrv\appcmd.exe set app
      "Default Web Site/servicemodelsamples" /enabledProtocols:http
      

      注释

      此命令必须以单行文本形式输入。

    2. 通过从管理员级命令提示符运行以下命令来删除 net.tcp 站点绑定:

      %windir%\system32\inetsrv\appcmd.exe set site "Default Web Site"
      --bindings.[protocol='net.tcp',bindingInformation='808:*']
      

      注释

      此命令必须键入为单行文本。

另请参阅