大网数据广告活动
.NET 与 C# 开发

使用 WCF 客户端访问服务

· 52CE 我爱测

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

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

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

  1. 编译服务代码。

  2. 生成 WCF 客户端代理。

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

有关详细信息,请参阅使用服务模型 Metadata Utility Tool (SvcUtil.exe) 可以手动生成 WCF 客户端代理ServiceModel Metadata Utility Tool (Svcutil.exe)。 此外可以在 Visual Studio 内生成 WCF 客户端代理使用添加服务引用功能。 若要使用上述两种方法中的一种生成 WCF 客户端代理,必须运行该服务。 如果服务是自承载服务,则必须运行主机。 如果服务是在 IIS/WAS 中承载的,则无需执行任何其他操作。

ServiceModel 元数据实用工具

ServiceModel Metadata Utility Tool (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]>

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

Important: 不安全的元数据请求中相同的任何不安全的网络请求的方式处理,也会带来一定的风险:如果您不能确定与你通信的终结点后向其颁发是,您检索的信息可能来自于恶意服务的元数据。

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

ServiceModel 元数据实用工具工具和添加服务引用在 Visual Studio 中生成以下 WCF 客户端类。 该类从 xref:System.ServiceModel.ClientBase`1 泛型类继承,并实现 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 客户端引发的许多异常是由服务上异常引起的。 以下是这种情况的一些示例:

当发生这些类型的异常时,解决问题的最佳方式是在服务端启用跟踪并确定服务端发生了何种异常。 有关跟踪的详细信息,请参阅跟踪并解决您的应用程序中使用跟踪。

请参阅

历史资料。此文对应 2019-08-18 的中文文档快照,适用产品版本和命令行为以原文为准。

来源与署名:Microsoft 和文档贡献者。固定版本原文。文档采用 CC BY 4.0,示例代码采用 MIT 授权。本站将 Markdown 转为 HTML,规范化链接,移除外部媒体和导航标记。

资料来源与适用范围

资料来源
Microsoft / .NET 中文文档历史快照
适用范围
历史中文快照 2019-08-18;详见原文产品版本;文档日期 03/30/2017
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-16
授权说明
CC BY 4.0(文档)/ MIT(示例代码)

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。