如何:使用 Windows Communication Foundation 客户端

这是创建基本 Windows Communication Foundation (WCF) 服务和可以调用该服务的客户端所需的六项任务中的最后一项任务。有关全部六项任务的概述,请参见入门教程主题。

在创建并配置了 Windows Communication Foundation (WCF) 代理后,就可以创建客户端实例,进而编译客户端应用程序并使用它与 WCF 服务进行通信。本主题描述创建和使用 WCF 客户端的过程。此过程执行三项操作:

  1. 创建 WCF 客户端。

  2. 从生成的代理调用服务操作。

  3. 在完成操作调用后关闭客户端。

在过程后面的示例中还提供了过程中所讨论的代码。此任务中的代码应放置在客户端项目中所生成的 Program 类的 Main() 方法中。

使用 Windows Communication Foundation 客户端

  1. 为要调用的服务的基址创建 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();
    
  2. 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);
    
  3. 在 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();

        }
    }
}

确保在尝试使用客户端之前服务正在运行。有关更多信息,请参见 如何:承载和运行基本的 Windows Communication Foundation 服务.

若要启动客户端,请在**“解决方案资源管理器”中右击“客户端”,然后依次选择“调试”“启动新实例”**。

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 客户端。有关疑难解答信息,请参见入门教程疑难解答

另请参见

任务

如何:创建 Windows Communication Foundation 客户端
如何:创建双工协定
如何:使用双工协定访问服务
入门示例
自承载

其他资源

生成客户端
入门教程
基本 WCF 编程