共用方式為


HOW TO:建立 Windows Communication Foundation 用戶端

這是在建立基本 Windows Communication Foundation (WCF) 服務以及可呼叫該服務的用戶端時,必須進行的六個工作中的第四個。 如需這六個工作的概觀,請參閱使用者入門教學課程主題。

本主題會說明如何從 WCF 服務擷取中繼資料,以建立可以存取服務的 WCF Proxy。 這個工作是透過 WCF 所提供的 ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 來完成。 這個工具會從服務取得中繼資料,並以您所選擇的語言產生 Proxy 的 Managed 原始程式碼檔。 除了建立用戶端 Proxy,此工具也會建立用戶端的組態檔,讓用戶端應用程式在其中一個端點與服務連線。

ms733133.note(zh-tw,VS.100).gif注意:
若不使用 ServiceModel 中繼資料公用程式工具 (Svcutil.exe),您還可在 Visual Studio 2010 中為用戶端專案加入服務參考,以建立用戶端 Proxy。

ms733133.Warning(zh-tw,VS.100).gif 注意:
從 Visual Studio 2010 的類別庫專案中呼叫 WCF 服務時,您可以使用 [加入服務參考] 功能來自動產生 Proxy 與相關聯的組態檔。 類別庫專案將不會使用組態檔。 您需要將組態檔複製到包含將呼叫類別庫之可執行檔的目錄。

用戶端應用程式會使用所產生的 Proxy 來建立 WCF 用戶端物件。 這項程序將於 HOW TO:使用 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 (version 4.0.0.0)],然後按一下 [確定]。

    ms733133.note(zh-tw,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 鍵啟動前述主題建立的服務。 如需詳細資訊,請參閱 HOW TO:裝載和執行基本 Windows Communication Foundation 服務.

  5. 執行下列步驟並使用適當的參數來執行ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 以建立用戶端程式碼與組態檔:

    1. 按一下 [開始] 功能表上的 [所有程式],再按一下 [Visual Studio 2010]。 按一下 [Visual Studio Tools],再按一下 [Visual Studio 2010 命令提示字元]。

    2. 巡覽至您要放置用戶端程式碼的目錄。 如果您是使用預設值來建立用戶端專案,則目錄為 C:\使用者\<user name>\我的文件\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
      

      根據預設,用戶端 Proxy 程式碼會在以服務為名的檔案中產生 (例如,此處的情況為 CalculatorService.cs 或 CalculatorService.vb,其副檔名視程式語言而定:Visual Basic 為 .vb 而 C# 則為 .cs)。 /out 參數會將用戶端 Proxy 檔案的名稱變更為 GeneratedProxy.cs。 /config 參數會將用戶端組態檔的名稱從預設的 Output.config 變更為 App.config。 請注意,這兩個檔案都將於 C:\使用者\<user name>\我的文件\Visual Studio 10\Projects\Service\Client 目錄中產生。

  6. 將產生的 Proxy 加入至 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) 用戶端。 請繼續進行 HOW TO:設定基本 Windows Communication Foundation 用戶端來設定用戶端。 如需疑難排解的詳細資訊,請參閱使用者入門教學課程疑難排解

另請參閱

工作

使用者入門範例
自我裝載
HOW TO:使用組態檔發行服務的中繼資料
HOW TO:使用 Svcutil.exe 來下載中繼資料文件

概念

ServiceModel 中繼資料公用程式工具 (Svcutil.exe)