如何:创建 Windows Communication Foundation 客户端

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

本主题描述如何检索 WCF 服务中的元数据,以及如何使用这些元数据创建可以访问该服务的 WCF 代理。此任务是通过使用 WCF 所提供的 ServiceModel 元数据实用工具 (Svcutil.exe) 完成的。此工具可以获取服务的元数据,并使用所选语言生成代理的托管源代码文件。除了创建客户端代理外,该工具还会为客户端创建配置文件,以使客户端应用程序能够连接至其某个终结点上的服务。

ms733133.note(zh-cn,VS.100).gif注意:
可以在 Visual Studio 2010 中向客户端项目添加服务引用,以便在不使用 ServiceModel 元数据实用工具 (Svcutil.exe) 的情况下创建客户端代理。

ms733133.Warning(zh-cn,VS.100).gif 警告:
当在 Visual Studio 2010 中从某个类库项目调用 WCF 服务时,可以使用添加服务引用功能自动生成代理和关联配置文件。该类库项目不会使用配置文件。需要将配置文件复制到包含调用该类库的可执行文件的目录中。

客户端应用程序使用生成的代理创建 WCF 客户端对象。如何:使用 Windows Communication Foundation 客户端中对此过程进行了描述。

在操作过程后面的示例中提供了用于此任务产生的客户端的代码。

创建 Windows Communication Foundation 客户端

  1. 通过执行以下步骤,在 Visual Studio 2010 中在当前解决方案中为客户端创建一个新项目:

    1. 在包含该服务的同一解决方案中的**“解决方案资源管理器”(位于右上角)中,右击当前解决方案(而不是项目),选择“添加”,然后选择“新项目”**。

    2. 在**“添加新项目”对话框中,选择“Visual Basic”“Visual C#”,选择“控制台应用程序”**模板,然后将其命名为 Client。使用默认的位置。

    3. 单击**“确定”**。

  2. 为项目添加对 System.ServiceModel.dll 的引用:

    1. 在**“解决方案资源管理器”中的“Client”项目下右击“引用”文件夹,然后选择“添加引用”**。

    2. 选择**“.NET”选项卡,从列表框中选择“System.ServiceModel.dll”(版本 4.0.0.0),然后单击“确定”**。

    ms733133.note(zh-cn,VS.100).gif注意:
    在使用命令行编译器(例如 Csc.exe 或 Vbc.exe)时,还必须提供程序集的路径。例如,默认情况下,在运行 Windows Vista 的计算机上,路径为:Windows\Microsoft.NET\Framework\v4.0。

  3. 在生成的 Program.cs 或 Program.vb 文件中为 System.ServiceModel 命名空间添加一个 using 语句(在 Visual Basic 中为 Imports)。

    Imports System.ServiceModel
    
    using System.ServiceModel;
    
  4. 在 Visual Studio 中,按 F5 启动在前面的主题中创建的服务。有关更多信息,请参见 如何:承载和运行基本的 Windows Communication Foundation 服务.

  5. 通过执行以下步骤,使用适当的开关运行ServiceModel 元数据实用工具 (Svcutil.exe) 以创建客户端代码和配置文件:

    1. 在**“开始”菜单上,单击“所有程序”,然后单击“Visual Studio 2010”。单击“Visual Studio 工具”,然后单击“Visual Studio 2010 命令提示”**。

    2. 导航到要放置客户端代码的目录。如果使用默认值创建客户端项目,则该目录为 C:\Users\<用户名>\My Documents\Visual Studio 10\Projects\Service\Client。

    3. 将命令行工具ServiceModel 元数据实用工具 (Svcutil.exe) 与适当的开关一起使用以创建客户端代码。下面的示例生成服务的代码文件和配置文件。

      svcutil.exe /language:vb /out:generatedProxy.vb /config:app.config https://localhost:8000/ServiceModelSamples/service
      
      svcutil.exe /language:cs /out:generatedProxy.cs /config:app.config https://localhost:8000/ServiceModelSamples/service
      

      默认情况下,将在一个以服务命名的文件(例如,在本示例中将为 CalculatorService.cs 或 CalculatorService.vb,其扩展名与编程语言相对应:.vb 对应于 Visual Basic,.cs 对应于 C#)中生成客户端代理代码。/out 开关会将客户端代理文件的名称更改为 GeneratedProxy.cs。/config 开关会将客户端配置文件的名称从默认的 Output.config 更改为 App.config。请注意,这些文件都是在 C:\Users\<用户名>\My Documents\Visual Studio 10\Projects\Service\Client 目录中生成的。

  6. 在 Visual Studio 中将生成的代理添加到该客户端项目中,方法是在**“解决方案资源管理器”中右击该客户端项目,选择“添加”,然后选择“现有项”。选择上面的步骤中生成的“generatedProxy”**文件。

示例

此示例演示由ServiceModel 元数据实用工具 (Svcutil.exe) 生成的客户端代码。

'------------------------------------------------------------------------------
' <auto-generated>
'     This code was generated by a tool.
'     Runtime Version:2.0.50727.1366
'
'     Changes to this file may cause incorrect behavior and will be lost if
'     the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------

Option Strict Off
Option Explicit On



<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0"),  _
 System.ServiceModel.ServiceContractAttribute([Namespace]:="http://Microsoft.ServiceModel.Samples", ConfigurationName:="ICalculator")>  _
Public Interface ICalculator
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")>  _
    Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")>  _
    Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")>  _
    Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double
    
    <System.ServiceModel.OperationContractAttribute(Action:="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction:="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")>  _
    Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double
End Interface

<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Public Interface ICalculatorChannel
    Inherits ICalculator, System.ServiceModel.IClientChannel
End Interface

<System.Diagnostics.DebuggerStepThroughAttribute(),  _
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
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
    
    Public Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Subtract
        Return MyBase.Channel.Subtract(n1, n2)
    End Function
    
    Public Function Multiply(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Multiply
        Return MyBase.Channel.Multiply(n1, n2)
    End Function
    
    Public Function Divide(ByVal n1 As Double, ByVal n2 As Double) As Double Implements ICalculator.Divide
        Return MyBase.Channel.Divide(n1, n2)
    End Function
End Class
//------------------------------------------------------------------------------
// <auto-generated>
//     This code was generated by a tool.
//     Runtime Version:2.0.50727.1366
//
//     Changes to this file may cause incorrect behavior and will be lost if
//     the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------



[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(Namespace="http://Microsoft.ServiceModel.Samples", ConfigurationName="ICalculator")]
public interface ICalculator
{
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
    double Add(double n1, double n2);
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
    double Subtract(double n1, double n2);
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
    double Multiply(double n1, double n2);
    
    [System.ServiceModel.OperationContractAttribute(Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide", ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
    double Divide(double n1, double n2);
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface ICalculatorChannel : ICalculator, System.ServiceModel.IClientChannel
{
}

[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
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);
    }
    
    public double Subtract(double n1, double n2)
    {
        return base.Channel.Subtract(n1, n2);
    }
    
    public double Multiply(double n1, double n2)
    {
        return base.Channel.Multiply(n1, n2);
    }
    
    public double Divide(double n1, double n2)
    {
        return base.Channel.Divide(n1, n2);
    }
}

现在您已经创建了一个 Windows Communication Foundation (WCF) 客户端。请继续执行如何:配置基本 Windows Communication Foundation 客户端中的步骤配置该客户端。有关疑难解答信息,请参见入门教程疑难解答

另请参见

任务

入门示例
自承载
如何:使用配置文件发布服务的元数据
如何:使用 Svcutil.exe 下载元数据文档

概念

ServiceModel 元数据实用工具 (Svcutil.exe)