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.