教學課程:使用 Windows Communication Foundation 用戶端

本教學課程說明建立基本 Windows Communication Foundation (WCF) 應用程式所需的五項工作中的最後一項。 如需教學課程的概觀,請參閱教學課程:開始使用 Windows Communication Foundation 應用程式

在建立並設定 Windows Communication Foundation (WCF) Proxy 之後,您將建立用戶端實例並編譯用戶端應用程式。 然後,您將用它來與 WCF 服務通訊。

在本教學課程中,您會了解如何:

  • 新增程式碼來使用 WCF 用戶端。
  • 測試 WCF 用戶端。

新增程式碼來使用 WCF 用戶端

用戶端程式代碼會執行下列步驟:

  • 具現化 WCF 用戶端。
  • 從產生的 Proxy 呼叫服務作業。
  • 在完成作業呼叫後關閉用戶端。

GettingStartedClient 專案中開啟 Program.csModule1.vb 檔案,並將其程式碼取代為下列程式碼:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GettingStartedClient.ServiceReference1;

namespace GettingStartedClient
{
    class Program
    {
        static void Main(string[] args)
        {
            //Step 1: Create an instance of the WCF proxy.
            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: Close the client to gracefully close the connection and clean up resources.
            Console.WriteLine("\nPress <Enter> to terminate the client.");
            Console.ReadLine();
            client.Close();
        }
    }
}
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports GettingStartedClient.ServiceReference1

Module Module1

    Sub Main()
        ' Step 1: Create an instance of the WCF proxy.
        Dim Client As New CalculatorClient()

        ' 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: Close the client to gracefully close the connection and clean up resources.
        Console.WriteLine()
        Console.WriteLine("Press <Enter> to terminate the client.")
        Console.ReadLine()
        Client.Close()

    End Sub

End Module

請注意匯入 GettingStartedClient.ServiceReference1using (對於 Visual C#) 或 Imports (對於 Visual Basic) 陳述式。 此陳述式會匯入 Visual Studio 使用 Add Service Reference 函式產生的程式碼。 該程式碼會具現化 WCF Proxy,並呼叫計算機服務所公開的每一項服務作業。 然後它會關閉 Proxy 並結束程式。

測試 WCF 用戶端

從 Visual Studio 測試應用程式

  1. 儲存並建置方案。

  2. 選取 [GettingStartedClient] 資料夾,然後從捷徑功能表中選取 [設定為啟始專案]

  3. 從 [啟始專案] 中,從下拉式清單選取 [GettingStartedClient],然後選取 [執行] 或按 F5

從命令提示字元測試應用程式

  1. 以管理員身分開啟命令提示字元,然後巡覽至 Visual Studio 方案目錄。

  2. 若要啟動服務:請輸入 GettingStartedHost\bin\Debug\GettingStartedHost.exe

  3. 若要啟動用戶端:請開啟另一個命令提示字元、巡覽至 Visual Studio 方案目錄,然後輸入 GettingStartedClient\bin\Debug\GettingStartedClient.exe

    GettingStartedHost.exe 會產生下列輸出:

    The service is ready.
    Press <Enter> to terminate the service.
    
    Received Add(100,15.99)
    Return: 115.99
    Received Subtract(145,76.54)
    Return: 68.46
    Received Multiply(9,81.25)
    Return: 731.25
    Received Divide(22,7)
    Return: 3.14285714285714
    

    GettingStartedClient.exe 會產生下列輸出:

    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 the client.
    

下一步

您現在已完成 WCF 入門教學課程中的所有工作。 在本教學課程中,您已了解如何:

在本教學課程中,您會了解如何:

  • 新增程式碼來使用 WCF 用戶端。
  • 測試 WCF 用戶端。

如果您在任何步驟中遇到問題或錯誤,請遵循疑難排解文章中的步驟來進行修正。