wsHttp 示例演示如何使用 Windows Communication Foundation(WCF)实现典型服务和典型客户端。 此示例由客户端控制台程序(client.exe)和 Internet 信息服务(IIS)托管的服务库组成。 该服务实现定义请求-回复通信模式的协定。 协定由 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 值配置会话在出错之前可以闲置的时间。
在<安全性>上,mode配置该值以确定应使用哪个安全模式。 在此示例中,正在使用消息安全性,这就是为什么在<安全性>中指定<消息>的原因。
运行示例时,操作请求和响应将显示在客户端控制台窗口中。 在客户端窗口中按 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.
设置、生成和运行示例
使用以下命令安装 ASP.NET 4.0。
%windir%\Microsoft.NET\Framework\v4.0.XXXXX\aspnet_regiis.exe /i /enable确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。