HOW TO:使用 Windows Communication Foundation 用戶端
這是在建立基本 Windows Communication Foundation (WCF) 服務,以及可呼叫該服務的用戶端時,必須進行的六個工作中的最後一個。 如需這六個工作的概觀,請參閱使用者入門教學課程主題。
在建立並設定 Windows Communication Foundation (WCF) Proxy 之後,就可以建立用戶端執行個體,也可以編譯用戶端應用程式並用於與 WCF 服務通訊。 本主題將說明建立及使用 WCF 用戶端的程序。 這個程序會執行三項工作:
建立 WCF 用戶端。
從產生的 Proxy 呼叫服務作業。
在完成作業呼叫後即關閉用戶端。
接續在程序後面的範例將同時提供程序中討論到的程式碼。 本工作的程式碼應置於用戶端專案產生之 Program
類別的 Main()
方法中。
若要使用 Windows Communication Foundation 用戶端
針對您要呼叫之服務的基底位址建立 EndpointAddress 執行個體,然後建立 WCF
Client
物件。' Step 1: Create an endpoint address and an instance of the WCF Client. Dim epAddress As New EndpointAddress("https://localhost:8000/ServiceModelSamples/Service/CalculatorService") Dim Client As New CalculatorClient(New WSHttpBinding(), epAddress)
//Step 1: Create an endpoint address and an instance of the WCF Client. CalculatorClient client = new CalculatorClient();
從
Client
中呼叫用戶端作業。'Step 2: Call the service operations. 'Call the Add service operation. Dim value1 As Double = 100D Dim value2 As Double = 15.99D Dim result As Double = Client.Add(value1, value2) Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result) 'Call the Subtract service operation. value1 = 145D value2 = 76.54D result = Client.Subtract(value1, value2) Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result) 'Call the Multiply service operation. value1 = 9D value2 = 81.25D result = Client.Multiply(value1, value2) Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result) 'Call the Divide service operation. value1 = 22D value2 = 7D result = Client.Divide(value1, value2) Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result)
// Step 2: Call the service operations. // Call the Add service operation. double value1 = 100.00D; double value2 = 15.99D; double result = client.Add(value1, value2); Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result); // Call the Subtract service operation. value1 = 145.00D; value2 = 76.54D; result = client.Subtract(value1, value2); Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result); // Call the Multiply service operation. value1 = 9.00D; value2 = 81.25D; result = client.Multiply(value1, value2); Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result); // Call the Divide service operation. value1 = 22.00D; value2 = 7.00D; result = client.Divide(value1, value2); Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
在 WCF 用戶端上呼叫
Close
,等候使用者按下 ENTER 鍵結束應用程式。' Step 3: Closing the client gracefully closes the connection and cleans up resources. Client.Close() Console.WriteLine() Console.WriteLine("Press <ENTER> to terminate client.") Console.ReadLine()
//Step 3: Closing the client gracefully closes the connection and cleans up resources. client.Close(); Console.WriteLine(); Console.WriteLine("Press <ENTER> to terminate client."); Console.ReadLine();
範例
下列範例示範如何建立 WCF 用戶端、如何呼叫用戶端的作業,以及如何在完成作業呼叫時立即關閉用戶端。
請將所產生的 WCF 用戶端和下列程式碼範例編譯為名為 Client.exe 的可執行檔。 在編譯程式碼時,請務必參考 System.ServiceModel。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Module Client
Sub Main()
' Step 1: Create an endpoint address and an instance of the WCF Client.
Dim epAddress As New EndpointAddress("https://localhost:8000/ServiceModelSamples/Service/CalculatorService")
Dim Client As New CalculatorClient(New WSHttpBinding(), epAddress)
'Step 2: Call the service operations.
'Call the Add service operation.
Dim value1 As Double = 100D
Dim value2 As Double = 15.99D
Dim result As Double = Client.Add(value1, value2)
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)
'Call the Subtract service operation.
value1 = 145D
value2 = 76.54D
result = Client.Subtract(value1, value2)
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result)
'Call the Multiply service operation.
value1 = 9D
value2 = 81.25D
result = Client.Multiply(value1, value2)
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result)
'Call the Divide service operation.
value1 = 22D
value2 = 7D
result = Client.Divide(value1, value2)
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result)
' Step 3: Closing the client gracefully closes the connection and cleans up resources.
Client.Close()
Console.WriteLine()
Console.WriteLine("Press <ENTER> to terminate client.")
Console.ReadLine()
End Sub
End Module
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
namespace ServiceModelSamples
{
class Client
{
static void Main()
{
//Step 1: Create an endpoint address and an instance of the WCF Client.
CalculatorClient client = new CalculatorClient();
// Step 2: Call the service operations.
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = client.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
// Call the Subtract service operation.
value1 = 145.00D;
value2 = 76.54D;
result = client.Subtract(value1, value2);
Console.WriteLine("Subtract({0},{1}) = {2}", value1, value2, result);
// Call the Multiply service operation.
value1 = 9.00D;
value2 = 81.25D;
result = client.Multiply(value1, value2);
Console.WriteLine("Multiply({0},{1}) = {2}", value1, value2, result);
// Call the Divide service operation.
value1 = 22.00D;
value2 = 7.00D;
result = client.Divide(value1, value2);
Console.WriteLine("Divide({0},{1}) = {2}", value1, value2, result);
//Step 3: Closing the client gracefully closes the connection and cleans up resources.
client.Close();
Console.WriteLine();
Console.WriteLine("Press <ENTER> to terminate client.");
Console.ReadLine();
}
}
}
在嘗試使用用戶端之前,請先確定服務正在執行中。 如需詳細資訊,請參閱 HOW TO:裝載和執行基本 Windows Communication Foundation 服務.
若要啟動用戶端,以滑鼠右鍵按一下 [方案總管] 中的 [Client],再依序選擇 [偵錯] 和 [開始新執行個體]。
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.
如果您看到這項輸出結果,表示您已順利完成教學課程。 這個範例將示範如何使用程式碼來設定 WCF 用戶端。 如需疑難排解的詳細資訊,請參閱使用者入門教學課程疑難排解。
另請參閱
工作
HOW TO:建立 Windows Communication Foundation 用戶端
HOW TO:建立雙工合約
HOW TO:使用雙工合約存取服務
使用者入門範例
自我裝載