多个协定

MultipleContracts 示例演示如何在服务上实现多个协定,以及如何配置终结点以与每个已实现的协定通信。 此示例基于入门指南。 服务已修改为定义两个协定,即 ICalculator 协定和 ICalculatorSession 协定。

注释

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

服务类实现 ICalculatorICalculatorSession 协定。 由于其中一个合同需要会话,因此服务使用 PerSession 实例模式以在会话生存期内保持状态。

已修改服务配置以定义两个终结点来公开每个协定。 ICalculator 终结点是使用 basicHttpBinding 在基地址上公开的。 ICalculatorSession 终结点是使用 wsHttpBinding(其 bindingConfiguration 属性设置为 BindingWithSession)在基地址/会话上公开的,如下面的示例配置中所示。

<service
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- ICalculator endpoint is exposed using BasicBinding at the base
       address provided by host:
       http://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- ICalculatorSession endpoint is exposed using BindingWithSession
       at {baseaddress}/session:
       http://localhost/servicemodelsamples/service.svc/session -->
  <endpoint address="session"
            binding="wsHttpBinding"
            bindingConfiguration="BindingWithSession"
           contract="Microsoft.ServiceModel.Samples.ICalculatorSession" />
  ...
</service>

生成的客户端代码现在包括原始 ICalculator 协定和新协定的 ICalculatorSession 客户端类。 已修改客户端配置和代码,以便与每个服务终结点上的相应合约进行通信。

客户端是控制台窗口应用程序(.exe)。 该服务由 Internet Information Services (IIS)托管。

客户端控制台窗口显示发送到每个终结点的操作,首先是基本终结点,然后是安全终结点。

设置、生成和运行示例

  1. 确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。

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

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