多個端點
多個端點範例會示範如何在服務上設定多個端點,以及如何從用戶端與每個端點進行通訊。此範例是以使用者入門範例為基礎。服務組態已修改成定義支援 ICalculator
合約的兩個端點,但是每個端點會位在不同的位址,同時使用不同的繫結。用戶端組態與程式碼已修改成可與這兩個服務端點進行通訊。
注意: |
---|
此範例的安裝程序與建置指示位於本主題的結尾。 |
服務 Web.config 檔已修改成定義兩個端點,每個端點都支援相同的 ICalculator
合約,但是分別位在不同的位址及使用不同繫結。第一個端點是定義位在基底位址且 basicHttpBinding 使用繫結,其並未啟用安全性。第二個端點是定義位在 {baseaddress}/secure 且使用 wsHttpBinding 繫結,其已透過搭配 Windows 驗證使用 WS-Security,因此預設為安全端點。
<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.
若要設定、建置及執行範例
若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示。
若要在單一或跨電腦的組態中執行本範例,請遵循執行 Windows Communication Foundation 範例中的指示。
Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.