共用方式為


使用 WCF 用戶端存取服務

建立服務之後,下一個步驟是建立 WCF 用戶端 Proxy。 用戶端應用程式會使用 WCF 用戶端 Proxy 與服務通訊。 用戶端應用程式通常會匯入服務的元數據,以產生可用來叫用服務的 WCF 用戶端程式代碼。

建立 WCF 用戶端的基本步驟包括:

  1. 編譯服務程序代碼。

  2. 產生 WCF 用戶端 Proxy。

  3. 實例化 WCF 客戶端代理。

如需詳細資訊,請參閱 ServiceModel 元數據公用程式工具(Svcutil.exe)。WCF 用戶端代理可以手動透過服務模型元數據公用程式工具(SvcUtil.exe)進行生成。 WCF 用戶端 Proxy 也可以使用 [新增服務參考 ] 功能,在 Visual Studio 內產生。 若要使用任一方法產生 WCF 用戶端 Proxy,服務必須執行。 如果服務是自行托管的,您必須運行主機。 如果服務裝載在 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 用戶端 Proxy 的項目,然後選取 [ 新增>服務參考]。 在 [ 新增服務參考] 對話框中,輸入您要呼叫之服務的 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 用戶端擲回的許多例外狀況都是由服務上的例外狀況所造成。 以下提供一些這類範例:

當這些類型的例外狀況發生時,解決問題的最佳方式是開啟服務端的追蹤,並判斷該處發生的例外狀況。 如需追蹤的詳細資訊,請參閱 追蹤使用追蹤來排除應用程式的問題

另請參閱