共用方式為


多個端點

MultipleEndpoints 範例示範如何在服務上設定多個端點,以及如何從用戶端與每個端點通訊。 此範例是以 用戶入門為基礎。 服務組態已修改為定義支援合約的 ICalculator 兩個端點,但每個端點都使用不同的系結在不同的位址。 用戶端組態和程式代碼已修改為與兩個服務端點通訊。

備註

此範例的安裝程式和建置指示位於本主題結尾。

服務 Web.config 檔案已修改為定義兩個端點,每個端點都支援相同的 ICalculator 合約,但在不同的位址使用不同的繫結。 第一個端點是在基礎位址使用未啟用安全性功能的basicHttpBinding系結來定義。 第二個 wsHttpBinding 端點是使用系結在 {baseaddress}/secure 中定義,而系結預設為安全,使用 WS-Security 搭配 Windows 驗證。

<service
    name="Microsoft.ServiceModel.Samples.CalculatorService"
    behaviorConfiguration="CalculatorServiceBehavior">
  <!-- This endpoint is exposed at the base address provided by host:
       http://localhost/servicemodelsamples/service.svc  -->
  <endpoint address=""
            binding="basicHttpBinding"
            contract="Microsoft.ServiceModel.Samples.ICalculator" />
  <!-- secure endpoint exposed at {base address}/secure:
       http://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="http://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="http://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 範例 執行One-Time 安裝程式。

  2. 若要建置解決方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例 中的指示。

  3. 若要在單一或跨計算機組態中執行範例,請遵循執行 Windows Communication Foundation 範例 中的指示。