使用 WCF 用戶端存取服務

建立服務之後,下一個步驟就是建立 WCF 用戶端 Proxy。 用戶端應用程式會使用 WCF 用戶端 Proxy 與服務通訊。 用戶端應用程式通常會匯入服務的中繼資料,以產生可用於叫用服務的 WCF 用戶端程式碼。

建立 WCF 用戶端的基本步驟包含下列各項:

  1. 編譯服務程式碼。

  2. 產生 WCF 用戶端 Proxy。

  3. 具現化 WCF 用戶端 Proxy。

使用 ServiceModel 中繼資料公用程式工具 (SvcUtil.exe) 可以手動產生 WCF 用戶端 Proxy。如需詳細資訊,請參閱 ServiceModel 中繼資料公用程式工具 (Svcutil.exe)。 在 Visual Studio 中,也可以使用 [加入服務參考] 功能產生 WCF 用戶端 Proxy。 若要使用上述任一方法來產生 WCF 用戶端 Proxy,服務都必須正在執行中。 如果服務是自我裝載的,則必須執行主機。 如果服務裝載於 IIS/WAS,就不需要執行任何其他動作。

ServiceModel 中繼資料公用程式工具

ServiceModel 中繼資料公用程式工具 (Svcutil.exe) 是從中繼資料產生程式碼的命令列工具。 下列用法是基本 Svcutil.exe 命令的範例。

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

或者,您也可以在檔案系統上,搭配 Web 服務描述語言 (WSDL) 和 XML 結構描述定義語言 (XSD) 檔案來使用 Svcutil.exe。

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 用戶端擲回的例外狀況是由服務上的例外狀況所導致。 以下提供一些這類範例:

發生這類例外狀況時,最佳的解決方式是開啟服務端的追蹤功能,並且判斷該處發生哪種例外狀況。 如需追蹤的詳細資訊,請參閱追蹤使用追蹤針對您的應用程式進行疑難排解

另請參閱