WSHttpBinding

wcf-wshttp 示例演示如何使用 Windows Communication Foundation (WCF) 实现典型的服务和典型的客户端。 此示例由客户端控制台程序 (client.exe) 和 Internet 信息服务 (IIS) 所承载的服务库组成。 该服务实现定义“请求-答复”通信模式的协定。 该协定由 ICalculator 接口定义,此接口公开数学运算(加、减、乘和除)。 客户端向给定的数学运算发出同步请求,服务使用结果进行回复。 客户端活动显示在控制台窗口中。

注意

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

此示例使用 <wsHttpBinding> 公开 ICalculator 协定。 此绑定的配置已在 Web.config 文件中展开。

<bindings>
  <wsHttpBinding>
    <!--The following is the expanded configuration section for a-->
    <!--WSHttpBinding. Each property is configured with the default-->
    <!--value. See the ReliableSession, TransactionFlow, -->
    <!--TransportSecurity, and MessageSecurity samples in the WS -->
    <!--directory to learn how to configure these features. -->
    <binding name="Binding1"
              bypassProxyOnLocal="false"
              transactionFlow="false"
              hostNameComparisonMode="StrongWildcard"
              maxBufferPoolSize="524288"
              maxReceivedMessageSize="65536"
              messageEncoding="Text"
              textEncoding="utf-8"
              useDefaultWebProxy="true"
              allowCookies="false">
      <reliableSession ordered="true"
                       inactivityTimeout="00:10:00"
                       enabled="false" />
      <security mode="Message">
        <message clientCredentialType="Windows"
                 negotiateServiceCredential="true"
                 algorithmSuite="Default"
                 establishSecurityContext="true" />
      </security>
    </binding>
  </wsHttpBinding>
</bindings>

在基 binding 元素上,通过 maxReceivedMessageSize 值可以配置传入消息的最大大小(以字节为单位)。 通过 hostNameComparisonMode 值,可以配置向服务多路分解消息时是否考虑主机名。 通过 messageEncoding 值可以配置是对消息使用文本还是 MTOM 编码。 通过 textEncoding 值可以为消息配置字符编码。 通过 bypassProxyOnLocal 值可以配置是否对本地通信使用 HTTP 协议。 通过 transactionFlow 值可以配置是否对当前事务进行流处理(如果为事务流配置了操作)。

<reliableSession> 元素上,启用的布尔值配置是否启用可靠会话。 ordered 值配置是否保留消息顺序。 inactivityTimeout 值配置会话在出错之前可以闲置的时间。

<security> 上,mode 值配置应使用的安全模式。 在本示例中使用了消息安全性,这也是在 <security> 内指定 <message> 的原因。

运行示例时,操作请求和响应将显示在客户端控制台窗口中。 在客户端窗口中按 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 版本的解决方案,请按照 Building the Windows Communication Foundation Samples中的说明进行操作。

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