WS 双向 Http

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

提示

本主题的最后介绍了此示例的设置过程和生成说明。

若要使用 WSDualHttpBinding 配置服务终结点,请在所示的终结点配置中指定绑定。

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

在客户端上,必须配置一个服务器可用来连接客户端的地址,如下面的示例配置所示。

<system.serviceModel>
  <client>
    <endpoint address=
         "https://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="https://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. 请确保已经执行了 Windows Communication Foundation 示例的一次性安装过程

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

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

    提示

    用跨计算机配置运行客户端时,确保用适当的计算机名称替换 endpoint 元素的 address 属性中的 localhost 和 wsDualHttpBinding 元素的 binding 元素的 clientBaseAddress 属性中的 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>
    

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.