多终结点

此多终结点示例演示如何在服务上配置多个终结点,以及如何从客户端与每个终结点通信。此示例基于入门示例。已经对服务配置进行了修改以定义两个支持 ICalculator 协定的终结点,但是这两个终结点位于使用不同绑定的不同地址。已经对客户端配置和代码进行了修改,以便与这两个服务终结点均进行通信。

提示

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

已经对服务的 Web.config 文件进行了修改以定义两个终结点,这两个终结点支持同一个 ICalculator 协定,但是位于使用不同绑定的不同地址。第一个终结点是在基址中使用 basicHttpBinding 绑定定义的,该绑定未启用安全性。第二个终结点是在 {baseaddress}/secure 中使用 wsHttpBinding 绑定定义的,默认情况下结合使用 WS-Security 和 Windows 身份验证来确保该绑定的安全。

<service 
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- This endpoint is exposed at the base address provided by host:       https://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- secure endpoint exposed at {base address}/secure:       https://localhost/servicemodelsamples/service.svc/secure -->
  <endpoint address="secure"
            binding="wsHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  ...
</service>

这两个终结点也都在客户端上进行了配置。这些终结点的命名方法允许调用方将所需的终结点名称传递到客户端的构造函数。

<client>
  <!-- Passing "basic" into the constructor of the CalculatorClient       class selects this endpoint.-->
  <endpoint name="basic"
            address="https://localhost/servicemodelsamples/service.svc" 
            binding="basicHttpBinding" 
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- Passing "secure" into the constructor of the CalculatorClient       class selects this endpoint.-->
  <endpoint name="secure"
address="https://localhost/servicemodelsamples/service.svc/secure" 
            binding="wsHttpBinding" 
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
</client>

客户端同时使用这两个终结点,如下面的代码中所示。

static void Main()
{
    // Create a client to the basic endpoint configuration.
    CalculatorClient client = new CalculatorClient("basic");
    Console.WriteLine("Communicate with basic endpoint.");
    // call operations
    DoCalculations(client);

    // Close the client and release resources.
    client.Close();

    // Create a client to the secure endpoint configuration.
    client = new CalculatorClient("secure");
    Console.WriteLine("Communicate with secure endpoint.");
    // Call operations.
    DoCalculations(client);

    // Close the client and release resources.
    client.Close();

    Console.WriteLine();
    Console.WriteLine("Press <ENTER> to terminate client.");
    Console.ReadLine();
}

运行客户端时,将显示与这两个终结点的交互结果。

Communicate with basic endpoint.
Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Communicate with secure endpoint.
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. 请确保已经执行了 Windows Communication Foundation 示例的一次性安装过程

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

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

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