WS 双向 Http

DualHttp 示例演示如何配置WSDualHttpBinding绑定。 此示例由 Internet 信息服务(IIS)托管的客户端控制台程序(.exe)和服务库(.dll)组成。 该服务实现双工协定。 协定由 ICalculatorDuplex 接口定义,该接口公开数学运算(加、减、乘和除)。 在此示例中, ICalculatorDuplex 接口允许客户端执行数学运算,从而通过会话计算正在运行的结果。 服务独立地返回ICalculatorDuplexCallback接口上的结果。 双向通信协议需要一个会话,因为必须建立一个上下文以关联客户端和服务之间正在发送的消息集合。 WSDualHttpBinding 绑定支持双工通信。

注释

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

若要使用WSDualHttpBinding配置服务终结点,请按照下述终结点配置中指定绑定。

<endpoint address=""
         binding="wsDualHttpBinding"
         contract="Microsoft.ServiceModel.Samples.ICalculatorDuplex" />

在客户端上,必须配置服务器可用于连接到客户端的地址,如以下示例配置所示。

<system.serviceModel>
  <client>
    <endpoint address=
         "http://localhost/servicemodelsamples/service.svc"
         binding="wsDualHttpBinding"
         bindingConfiguration="Binding1"
         contract="Microsoft.ServiceModel.Samples.ICalculatorDuplex" />
  </client>

  <bindings>
    <!-- Configure a WSDualHttpBinding that supports duplex -->
    <!-- communication. -->
    <wsDualHttpBinding>
      <binding name="Binding1"
               clientBaseAddress="http://localhost:8000/myClient/"
               useDefaultWebProxy="true"
               bypassProxyOnLocal="false">
      </binding>
    </wsDualHttpBinding>
  </bindings>
</system.serviceModel>

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

Press <ENTER> to terminate client once the output is displayed.

Result(100)
Result(50)
Result(882.5)
Result(441.25)
Equation(0 + 100 - 50 * 17.65 / 2 = 441.25)

运行示例时,会看到从服务发送的回调接口上返回到客户端的消息。 显示每个中间结果,完成所有操作后显示整个等式。 按 Enter 关闭客户端。

设置、生成和运行示例

  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 示例中的说明进行操作。

    重要

    用跨计算机配置运行客户端时,确保用适当的计算机名称替换 > 元素的 属性中的 localhost 和 > 元素的 > 元素的 属性中的 localhost,如下所示:

    <client>
        <endpoint name = ""
          address=
         "http://service_machine_name/servicemodelsamples/service.svc"
        />
    </client>
    ...
    <wsDualHttpBinding>
        <binding name="DuplexBinding" clientBaseAddress=
            "http://client_machine_name:8000/myClient/">
        </binding>
    </wsDualHttpBinding>