共用方式為


了解產生的用戶端程序代碼

ServiceModel 元數據公用程式工具 (Svcutil.exe) 會產生用戶端程式代碼和用戶端應用程式組態檔,以用於建置用戶端應用程式。 本主題提供標準服務合約案例所產生的程式碼範例導覽。 如需使用產生的程式代碼建置用戶端應用程式的詳細資訊,請參閱 WCF 用戶端概觀

概觀

如果您使用 Visual Studio 為您的專案產生 Windows Communication Foundation (WCF) 客戶端類型,您通常不需要檢查產生的用戶端程式代碼。 如果您未使用執行相同服務的開發環境,您可以使用 Svcutil.exe 之類的工具來產生用戶端程式代碼,然後使用該程式代碼來開發用戶端應用程式。

由於 Svcutil.exe 有許多選項可修改產生的類型資訊,因此本主題不會討論所有案例。 不過,下列標準工作牽涉到尋找產生的程序代碼:

  • 識別服務合約介面。

  • 識別 WCF 用戶端類別。

  • 識別數據類型。

  • 識別雙工服務的回呼合約。

  • 確認輔助服務合約通道介面。

尋找服務合約介面

若要找出用於建模服務合約的介面,請搜尋具備 System.ServiceModel.ServiceContractAttribute 屬性的介面。 通常,由於屬性本身上設定了其他屬性和明確屬性的存在,所以很難使用快速讀取來尋找此屬性。 請記住,服務合約介面和用戶端合約介面是兩種不同的類型。 下列程式代碼範例顯示原始服務合約。

[ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
  [OperationContractAttribute]
  [FaultContractAttribute(typeof(microsoft.wcf.documentation.SampleFault))]
  string SampleMethod(string msg);
}

下列程式代碼範例顯示與 Svcutil.exe所產生的相同服務合約。

[System.ServiceModel.ServiceContractAttribute(
  Namespace = "http://microsoft.wcf.documentation"
)]
public interface ISampleService
{
    [System.ServiceModel.OperationContractAttribute(
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
      ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
    )]
    [System.ServiceModel.FaultContractAttribute(
      typeof(microsoft.wcf.documentation.SampleFault),
      Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
    )]
    string SampleMethod(string msg);
}

您可以使用產生的服務合約介面以及 System.ServiceModel.ChannelFactory 類別來建立用來叫用服務作業的 WCF 通道物件。 如需詳細資訊,請參閱 如何:使用 ChannelFactory

尋找 WCF 用戶端類別

若要找出實作您想要使用之服務合約的 WCF 用戶端類別,請搜尋一個以 System.ServiceModel.ClientBase<TChannel> 為延伸的類別,其中的類型參數是您先前定位的服務合約介面,而且該類別會擴充此介面。 下列程式代碼範例顯示 ClientBase<TChannel> 類型的 ISampleService類別。

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public partial class SampleServiceClient : System.ServiceModel.ClientBase<ISampleService>, ISampleService
{

    public SampleServiceClient()
    {
    }

    public SampleServiceClient(string endpointConfigurationName)
        :
            base(endpointConfigurationName)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, string remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(string endpointConfigurationName, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(endpointConfigurationName, remoteAddress)
    {
    }

    public SampleServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress)
        :
            base(binding, remoteAddress)
    {
    }
    public string SampleMethod(string msg)
    {
        return base.Channel.SampleMethod(msg);
    }
}

您可以建立它的新實例,並呼叫它實作的方法,以使用此 WCF 用戶端類別。 這些方法會呼叫與其互動並且是經過專門設計和設定的服務操作。 如需詳細資訊,請參閱 WCF 用戶端概觀

備註

當 SvcUtil.exe 產生 WCF 用戶端類別時,它會將 加入 DebuggerStepThroughAttribute 至客戶端類別,以防止調試程式逐步執行 WCF 用戶端類別。

尋找數據類型

若要在產生的程式代碼中找出數據類型,最基本的機制是識別合約中指定的類型名稱,並搜尋該程式碼中該類型宣告。 例如,下列合約指定 SampleMethod 可以傳回 類型的 microsoft.wcf.documentation.SampleFaultSOAP錯誤。

[System.ServiceModel.OperationContractAttribute(
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethod",
  ReplyAction = "http://microsoft.wcf.documentation/ISampleService/SampleMethodResponse"
)]
[System.ServiceModel.FaultContractAttribute(
  typeof(microsoft.wcf.documentation.SampleFault),
  Action = "http://microsoft.wcf.documentation/ISampleService/SampleMethodSampleFaultFault"
)]
string SampleMethod(string msg);

搜尋SampleFault以定位下列類型宣告。

[assembly: System.Runtime.Serialization.ContractNamespaceAttribute(
  "http://microsoft.wcf.documentation",
  ClrNamespace = "microsoft.wcf.documentation"
)]
namespace microsoft.wcf.documentation
{
    using System.Runtime.Serialization;

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Runtime.Serialization", "3.0.0.0")]
    [System.Runtime.Serialization.DataContractAttribute()]
    public partial class SampleFault : object, System.Runtime.Serialization.IExtensibleDataObject
    {
        private System.Runtime.Serialization.ExtensionDataObject extensionDataField;
        private string FaultMessageField;

        public System.Runtime.Serialization.ExtensionDataObject ExtensionData
        {
            get
            {
                return this.extensionDataField;
            }
            set
            {
                this.extensionDataField = value;
            }
        }

        [System.Runtime.Serialization.DataMemberAttribute()]
        public string FaultMessage
        {
            get
            {
                return this.FaultMessageField;
            }
            set
            {
                this.FaultMessageField = value;
            }
        }
    }
}

在這裡情況下,資料類型是用戶端上特定例外狀況所擲回的詳細數據型別, FaultException<TDetail> 其中詳細數據型別參數為 microsoft.wcf.documentation.SampleFault。 如需數據類型的詳細資訊,請參閱 在服務合約中指定數據傳輸。 如需在用戶端中處理例外狀況的詳細資訊,請參閱 傳送和接收錯誤

尋找雙工服務的回調合約

如果您找到合約介面為 ServiceContractAttribute.CallbackContract 屬性指定值的服務契約,則表示該契約為雙工契約。 雙工合約要求用戶端應用程式建立一個回呼類別,該類別需要實作回呼合約,並將此類別的實例傳遞給 System.ServiceModel.DuplexClientBase<TChannel>System.ServiceModel.DuplexChannelFactory<TChannel>,以與服務進行通訊。 如需雙工用戶端的詳細資訊,請參閱 如何:使用雙工合約存取服務

下列合約指定為類型SampleDuplexHelloCallback的回呼合約。

[System.ServiceModel.ServiceContractAttribute(
  Namespace="http://microsoft.wcf.documentation",
  ConfigurationName="SampleDuplexHello",
  CallbackContract=typeof(SampleDuplexHelloCallback),
  SessionMode=System.ServiceModel.SessionMode.Required
)]
public interface SampleDuplexHello
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Hello"
    )]
    void Hello(string greeting);
  }
    <System.ServiceModel.OperationContractAttribute(IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Hello")> _
    Sub Hello(ByVal greeting As String)
End Interface 'SampleDuplexHello

搜尋該回呼合約會找出用戶端應用程式必須實作的下列介面。

  [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
public interface SampleDuplexHelloCallback
{
  [System.ServiceModel.OperationContractAttribute(
      IsOneWay=true,
      Action="http://microsoft.wcf.documentation/SampleDuplexHello/Reply"
    )]
    void Reply(string responseToGreeting);
  }
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")>  _
Public Interface SampleDuplexHelloCallback
    <System.ServiceModel.OperationContractAttribute( _
        IsOneWay:=True, _
        Action:="http://microsoft.wcf.documentation/SampleDuplexHello/Reply")> _
    Sub Reply(ByVal responseToGreeting As String)
End Interface 'SampleDuplexHelloCallback

尋找服務合約通道介面

搭配服務合約介面使用 ChannelFactory 類別時,您必須轉換成 System.ServiceModel.IClientChannel 介面,才能明確開啟、關閉或中止通道。 為了更輕鬆地使用,Svcutil.exe 工具也會產生協助程式介面,該介面會同時實作服務合約介面,並 IClientChannel 可讓您與用戶端通道基礎結構互動,而不需要轉換。 下列程式代碼顯示實作上述服務合約的協助程式用戶端通道定義。

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

另請參閱