使用 WCF 客户端访问服务

创建服务后,下一步是创建 WCF 客户端代理。 客户端应用程序使用 WCF 客户端代理与服务通信。 客户端应用程序通常导入服务的元数据,以生成可用于调用服务的 WCF 客户端代码。

创建 WCF 客户端的基本步骤包括:

  1. 编译服务代码。

  2. 生成 WCF 客户端代理。

  3. 实例化 WCF 客户端代理。

可以使用服务模型元数据实用工具工具(SvcUtil.exe)手动生成 WCF 客户端代理,有关详细信息,请参阅 ServiceModel 元数据实用工具工具(Svcutil.exe)。 WCF 客户端代理还可以使用 “添加服务引用 ”功能在 Visual Studio 中生成。 若要使用任一方法生成 WCF 客户端代理,服务必须正在运行。 如果服务是自行托管的,则必须管理该主机。 如果服务托管在 IIS/WAS 中,则无需执行任何其他作业。

ServiceModel 元数据实用工具

ServiceModel 元数据实用工具工具(Svcutil.exe)是一种命令行工具,用于从元数据生成代码。 以下用法是基本 Svcutil.exe 命令的示例。

Svcutil.exe <service's Metadata Exchange (MEX) address or HTTP GET address>

或者,可以将 Svcutil.exe 与文件系统上的 Web 服务描述语言(WSDL)和 XML 架构定义语言(XSD)文件配合使用。

Svcutil.exe <list of WSDL and XSD files on file system>

结果是一个代码文件,其中包含客户端应用程序可用于调用服务的 WCF 客户端代码。

还可以使用该工具生成配置文件。

Svcutil.exe <file1 [,file2]>

如果只提供一个文件名,则为输出文件的名称。 如果提供了两个文件名,则第一个文件是一个输入配置文件,其内容与生成的配置合并,并写出到第二个文件中。 有关配置的详细信息,请参阅 为服务配置绑定

重要

不安全的元数据请求以与任何不安全的网络请求相同的方式构成某些风险:如果你不确定与之通信的终结点是谁,则你检索的信息可能是来自恶意服务的元数据。

在 Visual Studio 中添加服务引用

运行服务后,右键单击将包含 WCF 客户端代理的项目,然后选择 “添加>服务引用”。 在 “添加服务引用”对话框中,键入要调用的服务的 URL,然后单击“ 转到 ”按钮。 该对话框将显示指定地址处可用的服务列表。 双击服务以查看可用的协定和操作,指定生成代码的命名空间,然后单击确定按钮。

示例:

下面的代码示例演示为服务创建的服务协定。

// Define a service contract.
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    // Other methods are not shown here.
}
' Define a service contract.
<ServiceContract(Namespace:="http://Microsoft.ServiceModel.Samples")> _
Public Interface ICalculator
    <OperationContract()>  _
    Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
    ' Other methods are not shown here.
End Interface

Visual Studio 中的 ServiceModel 元数据实用工具工具和 添加服务引用 生成以下 WCF 客户端类。 该类继承自泛型 ClientBase<TChannel> 类并实现 ICalculator 接口。 该工具还会生成 ICalculator 接口(此处未显示)。

public partial class CalculatorClient : System.ServiceModel.ClientBase<ICalculator>, ICalculator
{
    public CalculatorClient()
    {}

    public CalculatorClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
    {}

    public CalculatorClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {}

    public CalculatorClient(string endpointConfigurationName,
        System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
    {}

    public CalculatorClient(System.ServiceModel.Channels.Binding binding,
        System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
    {}

    public double Add(double n1, double n2)
    {
        return base.Channel.Add(n1, n2);
    }
}
Partial Public Class CalculatorClient
    Inherits System.ServiceModel.ClientBase(Of ICalculator)
    Implements ICalculator

    Public Sub New()
        MyBase.New
    End Sub

    Public Sub New(ByVal endpointConfigurationName As String)
        MyBase.New(endpointConfigurationName)
    End Sub

    Public Sub New(ByVal endpointConfigurationName As String, ByVal remoteAddress As String)
        MyBase.New(endpointConfigurationName, remoteAddress)
    End Sub

    Public Sub New(ByVal endpointConfigurationName As String,
        ByVal remoteAddress As System.ServiceModel.EndpointAddress)
        MyBase.New(endpointConfigurationName, remoteAddress)
    End Sub

    Public Sub New(ByVal binding As System.ServiceModel.Channels.Binding,
        ByVal remoteAddress As System.ServiceModel.EndpointAddress)
        MyBase.New(binding, remoteAddress)
    End Sub

    Public Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
        Implements ICalculator.Add
        Return MyBase.Channel.Add(n1, n2)
    End Function
End Class

使用 WCF 客户端

若要使用 WCF 客户端,请创建 WCF 客户端的实例,然后调用其方法,如以下代码所示。

// Create a client object with the given client endpoint configuration.
CalculatorClient calcClient = new CalculatorClient("CalculatorEndpoint");
// Call the Add service operation.
double value1 = 100.00D;
double value2 = 15.99D;
double result = calcClient.Add(value1, value2);
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);
' Create a client object with the given client endpoint configuration.
Dim calcClient As CalculatorClient = _
New CalculatorClient("CalculatorEndpoint")

' Call the Add service operation.
Dim value1 As Double = 100.00D
Dim value2 As Double = 15.99D
Dim result As Double = calcClient.Add(value1, value2)
Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result)

调试客户端引发的异常

WCF 客户端引发的许多异常是由服务上的异常引起的。 这些功能的示例包括:

发生这些类型的异常时,解决问题的最佳方式是在服务端打开跟踪并确定发生什么异常。 有关跟踪的详细信息,请参阅 跟踪 和使用 跟踪对应用程序进行故障排除

另请参阅